コード例 #1
0
int main(int argc, char *argv[])
{
    char buff[256];
    int len;

	log_init();
	//g_log_context.log_level = LOG_DEBUG;
	g_log_context.log_level = LOG_INFO;
    log_take_over_stderr();
    log_take_over_stdout();
    log_set_compress_log_flags(LOG_COMPRESS_FLAGS_ENABLED | LOG_COMPRESS_FLAGS_NEW_THREAD);
    
    printf("sizeof(LogContext): %d, time_precision: %d, compress_log_flags: %d, "
            "use_file_write_lock: %d\n", (int)sizeof(LogContext),
            g_log_context.time_precision,
            g_log_context.compress_log_flags,
            g_log_context.use_file_write_lock);

    log_it_ex(&g_log_context, LOG_DEBUG,
            "by log_it_ex, timestamp: %d", (int)time(NULL));

    len = sprintf(buff, "this is by log_it_ex1, "
            "timestamp: %d", (int)time(NULL));
    log_it_ex1(&g_log_context, LOG_INFO, buff, len);

	return 0;
}
コード例 #2
0
static int storage_do_recovery(const char *pBasePath, StorageBinLogReader *pReader, \
		ConnectionInfo *pSrcStorage)
{
	ConnectionInfo *pTrackerServer;
	ConnectionInfo *pStorageConn;
	FDFSTrunkFullInfo trunk_info;
	StorageBinLogRecord record;
	int record_length;
	int result;
	int log_level;
	int count;
	int store_path_index;
	int64_t file_size;
	int64_t total_count;
	int64_t success_count;
	bool bContinueFlag;
	char local_filename[MAX_PATH_SIZE];
	char src_filename[MAX_PATH_SIZE];

	pTrackerServer = g_tracker_group.servers;
	count = 0;
	total_count = 0;
	success_count = 0;
	result = 0;

	logInfo("file: "__FILE__", line: %d, " \
		"disk recovery: recovering files of data path: %s ...", \
		__LINE__, pBasePath);

	bContinueFlag = true;
	while (bContinueFlag)
	{
	if ((pStorageConn=tracker_connect_server(pSrcStorage, &result)) == NULL)
	{
		sleep(5);
		continue;
	}

	while (g_continue_flag)
	{
		result=storage_binlog_read(pReader, &record, &record_length);
		if (result != 0)
		{
			if (result == ENOENT)
			{
				result = 0;
			}
			bContinueFlag = false;
			break;
		}

		total_count++;
		if (record.op_type == STORAGE_OP_TYPE_SOURCE_CREATE_FILE
		 || record.op_type == STORAGE_OP_TYPE_REPLICA_CREATE_FILE)
		{
			bool bTrunkFile;

			if (fdfs_is_trunk_file(record.filename, \
					record.filename_len))
			{
			char *pTrunkPathEnd;
			char *pLocalFilename;

			bTrunkFile = true;
			if (fdfs_decode_trunk_info(record.store_path_index, \
				record.true_filename, record.true_filename_len,\
				&trunk_info) != 0)
			{
				pReader->binlog_offset += record_length;
				count++;
				continue;
			}

			trunk_get_full_filename(&trunk_info, \
                		local_filename, sizeof(local_filename));

			pTrunkPathEnd = strrchr(record.filename, '/');
			pLocalFilename = strrchr(local_filename, '/');
			if (pTrunkPathEnd == NULL || pLocalFilename == NULL)
			{
				pReader->binlog_offset += record_length;
				count++;
				continue;
			}
			sprintf(pTrunkPathEnd + 1, "%s", pLocalFilename + 1);
			}
			else
			{
			bTrunkFile = false;
			sprintf(local_filename, "%s/data/%s", \
				g_fdfs_store_paths.paths[record.store_path_index], \
				record.true_filename);
			}

			result = storage_download_file_to_file(pTrackerServer, \
					pStorageConn, g_group_name, \
					record.filename, local_filename, \
					&file_size);
			if (result == 0)
			{
				if (!bTrunkFile)
				{
					set_file_utimes(local_filename, \
						record.timestamp);
				}

				success_count++;
			}
			else if (result != ENOENT)
			{
				break;
			}
		}
		else if (record.op_type == STORAGE_OP_TYPE_SOURCE_CREATE_LINK
		      || record.op_type == STORAGE_OP_TYPE_REPLICA_CREATE_LINK)
		{
			if (record.src_filename_len == 0)
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid binlog line, filename: %s, " \
					"expect src filename", __LINE__, \
					record.filename);
				result = EINVAL;
				bContinueFlag = false;
				break;
			}

			if ((result=storage_split_filename_ex(record.filename, \
				&record.filename_len, record.true_filename, \
				&store_path_index)) != 0)
			{
				bContinueFlag = false;
				break;
			}
			sprintf(local_filename, "%s/data/%s", \
				g_fdfs_store_paths.paths[store_path_index], \
				record.true_filename);

			if ((result=storage_split_filename_ex( \
				record.src_filename, &record.src_filename_len,\
				record.true_filename, &store_path_index)) != 0)
			{
				bContinueFlag = false;
				break;
			}
			sprintf(src_filename, "%s/data/%s", \
				g_fdfs_store_paths.paths[store_path_index], \
				record.true_filename);
			if (symlink(src_filename, local_filename) == 0)
			{
				success_count++;
			}
			else
			{
				result = errno != 0 ? errno : ENOENT;
				if (result == ENOENT || result == EEXIST)
				{
					log_level = LOG_DEBUG;
				}
				else
				{
					log_level = LOG_ERR;
				}

				log_it_ex(&g_log_context, log_level, \
					"file: "__FILE__", line: %d, " \
					"link file %s to %s fail, " \
					"errno: %d, error info: %s", __LINE__,\
					src_filename, local_filename, \
					result, STRERROR(result));

				if (result != ENOENT && result != EEXIST)
				{
					bContinueFlag = false;
					break;
				}
			}
		}
		else
		{
			logError("file: "__FILE__", line: %d, " \
				"invalid file op type: %d", \
				__LINE__, record.op_type);
			result = EINVAL;
			bContinueFlag = false;
			break;
		}

		pReader->binlog_offset += record_length;
		count++;
		if (count == 1000)
		{
			logDebug("file: "__FILE__", line: %d, " \
				"disk recovery: recover path: %s, " \
				"file count: %"PRId64 \
				", success count: %"PRId64, \
				__LINE__, pBasePath, total_count, \
				success_count);
			recovery_write_to_mark_file(pBasePath, pReader);
			count = 0;
		}
	}

	tracker_disconnect_server_ex(pStorageConn, result != 0);
	if (count > 0)
	{
		recovery_write_to_mark_file(pBasePath, pReader);
		count = 0;

		logInfo("file: "__FILE__", line: %d, " \
			"disk recovery: recover path: %s, " \
			"file count: %"PRId64 \
			", success count: %"PRId64, \
			__LINE__, pBasePath, total_count, success_count);
	}
	else
	{
		sleep(5);
	}
	}

	if (result == 0)
	{
		logInfo("file: "__FILE__", line: %d, " \
			"disk recovery: recover files of data path: %s done", \
			__LINE__, pBasePath);
	}

	return result;
}