void CheckpointService::runCheckpoint(
	EventContext &ec, int32_t mode, uint32_t flag) {
	if ((lastMode_ == CP_UNDEF && mode != CP_AFTER_RECOVERY) ||
		lastMode_ == CP_SHUTDOWN ||
		(mode != CP_SHUTDOWN && requestedShutdownCheckpoint_)  
		) {
		GS_TRACE_WARNING(CHECKPOINT_SERVICE, GS_TRACE_CP_CHECKPOINT_CANCELLED,
			"Checkpoint cancelled by status (mode="
				<< checkpointModeToString(mode)
				<< ", lastMode=" << checkpointModeToString(lastMode_)
				<< ", shutdownRequested=" << requestedShutdownCheckpoint_
				<< ")");
		return;
	}

	currentCpGrpId_ = 0;
	errorOccured_ = false;

	struct CheckpointDataCleaner {
		CheckpointDataCleaner(CheckpointService &service,
			ChunkManager &chunkManager, EventContext &ec)
			: service_(service),
			  chunkManager_(chunkManager),
			  ec_(ec),
			  workerId_(ec.getWorkerId()) {}

		~CheckpointDataCleaner() {
			if (workerId_ == 0) {
				for (PartitionGroupId pgId = 0;
					 pgId < service_.pgConfig_.getPartitionGroupCount();
					 ++pgId) {
					const PartitionId startPId =
						service_.pgConfig_.getGroupBeginPartitionId(pgId);
					const CheckpointId cpId = 0;  
					try {
						service_.executeOnTransactionService(ec_,
							CLEANUP_CP_DATA, CP_UNDEF, startPId, cpId,
							false);  
					}
					catch (...) {
					}
				}
				try {
					service_.endTime_ =
						util::DateTime::now(false).getUnixTime();
					service_.pendingPartitionCount_ = 0;
				}
				catch (...) {
				}
			}
		}

		CheckpointService &service_;
		ChunkManager &chunkManager_;
		EventContext &ec_;
		uint32_t workerId_;
	} cpDataCleaner(*this, *chunkManager_, ec);

	startTime_ = 0;
	endTime_ = 0;

	lastMode_ = mode;
	startTime_ = util::DateTime::now(false).getUnixTime();
	pendingPartitionCount_ = pgConfig_.getPartitionCount();

	GS_TRACE_INFO(CHECKPOINT_SERVICE, GS_TRACE_CP_STATUS,
		"[CP_START] mode=" << checkpointModeToString(mode));

	if (parallelCheckpoint_) {
		groupCheckpointStatus_.assign(
			pgConfig_.getPartitionGroupCount(), GROUP_CP_COMPLETED);
		PartitionGroupId pgId = 1;
		try {
			for (; pgId < pgConfig_.getPartitionGroupCount(); ++pgId) {
				PartitionId topPId = pgConfig_.getGroupBeginPartitionId(pgId);
				Event requestEvent(ec, CP_REQUEST_GROUP_CHECKPOINT, topPId);
				EventByteOutStream out = requestEvent.getOutStream();
				out << mode;
				out << flag;
				groupCheckpointStatus_[pgId] = GROUP_CP_RUNNING;
				ee_.add(requestEvent);
			}
			pgId = 0;
			groupCheckpointStatus_[pgId] = GROUP_CP_RUNNING;
			runGroupCheckpoint(pgId, ec, mode, flag);
		}
		catch (...) {
			groupCheckpointStatus_[pgId] = GROUP_CP_COMPLETED;
			waitAllGroupCheckpointEnd();
			throw;
		}
		waitAllGroupCheckpointEnd();
	}
	else {
		for (PartitionGroupId pgId = 0;
			 pgId < pgConfig_.getPartitionGroupCount(); ++pgId) {
			runGroupCheckpoint(pgId, ec, mode, flag);
		}
	}

	if (mode == CP_AFTER_RECOVERY) {
		clusterService_->requestCompleteCheckpoint(ec, ec.getAllocator(), true);
		RecoveryManager::setProgressPercent(100);
	}
	if (mode == CP_SHUTDOWN) {
		clusterService_->requestCompleteCheckpoint(
			ec, ec.getAllocator(), false);
	}

	GS_TRACE_INFO(CHECKPOINT_SERVICE, GS_TRACE_CP_STATUS,
		"[CP_END] mode=" << checkpointModeToString(mode)
						 << ", commandElapsedMillis="
						 << getLastDuration(util::DateTime::now(false)));

	pendingPartitionCount_ = 0;

	if (mode == CP_NORMAL) {
		totalNormalCpOperation_++;
	}
	else if (mode == CP_REQUESTED) {
		totalRequestedCpOperation_++;
	}
}
Esempio n. 2
0
void ZlibUtils::compressData(
		const uint8_t* src, uint32_t srcSize,
		uint8_t* dest, uint32_t &destSize)
{
	int32_t flush = Z_FINISH;

	deflateStream_.zalloc = Z_NULL;
	deflateStream_.zfree = Z_NULL;
	deflateStream_.opaque = Z_NULL;

	int32_t ret = deflateInit(&deflateStream_, COMPRESS_LEVEL);
	if (ret != Z_OK) {
		if (ret == Z_MEM_ERROR) {
			if (isFirstTime_) {
				GS_TRACE_WARNING(ZLIB_UTILS, GS_TRACE_CM_COMPRESSION_FAILED,
						"error occured in deflateInit.");
				isFirstTime_ = false;
			}
			destSize = srcSize;
			compressionErrorCount_++;
			return;
		}
		GS_THROW_USER_ERROR(GS_ERROR_CM_COMPRESSION_FAILED,
							  "deflateInit failed.");
	}

	deflateStream_.avail_in = srcSize;
	deflateStream_.avail_out = destSize;
	deflateStream_.next_in = (Bytef*)src;
	deflateStream_.next_out = (Bytef*)dest;

	do {
		ret = deflate(&deflateStream_, flush);
	} while (ret == Z_OK);

	if (ret != Z_STREAM_END) {
		if (isFirstTime_) {
			GS_TRACE_ERROR(ZLIB_UTILS, GS_TRACE_CM_COMPRESSION_FAILED,
						   "error occured in deflate.");
			isFirstTime_ = false;
		}
		destSize = srcSize;
		compressionErrorCount_++;
		return;
	} else {
		destSize = static_cast<uint32_t>(deflateStream_.total_out);
		if (srcSize < destSize) {
			destSize = srcSize;
		}
	}

	ret = deflateEnd(&deflateStream_);
	if (ret != Z_OK) {
		if (isFirstTime_) {
			GS_TRACE_ERROR(ZLIB_UTILS, GS_TRACE_CM_COMPRESSION_FAILED,
						   "error occured in deflateEnd.");
		}
		destSize = srcSize;
		compressionErrorCount_++;
		return;
	}
}