Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.jraft.rhea;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alipay.sofa.jraft.rhea.client.RheaKVStore;
import com.alipay.sofa.jraft.rhea.cmd.pd.BaseRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.BaseResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.CreateRegionIdRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.CreateRegionIdResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.GetClusterInfoRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.GetClusterInfoResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.GetStoreIdRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.GetStoreIdResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.GetStoreInfoRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.GetStoreInfoResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.RegionHeartbeatResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.SetStoreInfoRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.SetStoreInfoResponse;
import com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatRequest;
import com.alipay.sofa.jraft.rhea.cmd.pd.StoreHeartbeatResponse;
import com.alipay.sofa.jraft.rhea.errors.Errors;
import com.alipay.sofa.jraft.rhea.metadata.Cluster;
import com.alipay.sofa.jraft.rhea.metadata.Instruction;
import com.alipay.sofa.jraft.rhea.metadata.Store;
import com.alipay.sofa.jraft.rhea.options.PlacementDriverServerOptions;
import com.alipay.sofa.jraft.rhea.pipeline.event.RegionPingEvent;
import com.alipay.sofa.jraft.rhea.pipeline.event.StorePingEvent;
import com.alipay.sofa.jraft.rhea.pipeline.handler.LogHandler;
import com.alipay.sofa.jraft.rhea.pipeline.handler.PlacementDriverTailHandler;
import com.alipay.sofa.jraft.rhea.util.StackTraceUtil;
import com.alipay.sofa.jraft.rhea.util.concurrent.CallerRunsPolicyWithReport;
import com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory;
import com.alipay.sofa.jraft.rhea.util.pipeline.DefaultHandlerInvoker;
import com.alipay.sofa.jraft.rhea.util.pipeline.DefaultPipeline;
import com.alipay.sofa.jraft.rhea.util.pipeline.Handler;
import com.alipay.sofa.jraft.rhea.util.pipeline.HandlerInvoker;
import com.alipay.sofa.jraft.rhea.util.pipeline.Pipeline;
import com.alipay.sofa.jraft.rhea.util.pipeline.future.PipelineFuture;
import com.alipay.sofa.jraft.util.JRaftServiceLoader;
import com.alipay.sofa.jraft.util.Requires;
import com.alipay.sofa.jraft.util.ThreadPoolUtil;
/**
*
* @author jiachun.fjc
*/
public class DefaultPlacementDriverService implements PlacementDriverService, LeaderStateListener {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPlacementDriverService.class);
private final RheaKVStore rheaKVStore;
private MetadataStore metadataStore;
private HandlerInvoker pipelineInvoker;
private Pipeline pipeline;
private volatile boolean isLeader;
private boolean started;
public DefaultPlacementDriverService(RheaKVStore rheaKVStore) {
this.rheaKVStore = rheaKVStore;
}
@Override
public synchronized boolean init(final PlacementDriverServerOptions opts) {
if (this.started) {
LOG.info("[DefaultPlacementDriverService] already started.");
return true;
}
Requires.requireNonNull(opts, "placementDriverServerOptions");
this.metadataStore = new DefaultMetadataStore(this.rheaKVStore);
final ThreadPoolExecutor threadPool = createPipelineExecutor(opts);
if (threadPool != null) {
this.pipelineInvoker = new DefaultHandlerInvoker(threadPool);
}
this.pipeline = new DefaultPipeline(); //
initPipeline(this.pipeline);
LOG.info("[DefaultPlacementDriverService] start successfully, options: {}.", opts);
return this.started = true;
}
@Override
public synchronized void shutdown() {
if (!this.started) {
return;
}
try {
if (this.pipelineInvoker != null) {
this.pipelineInvoker.shutdown();
}
invalidLocalCache();
} finally {
this.started = false;
LOG.info("[DefaultPlacementDriverService] shutdown successfully.");
}
}
@Override
public void handleStoreHeartbeatRequest(final StoreHeartbeatRequest request,
final RequestProcessClosure closure) {
final StoreHeartbeatResponse response = new StoreHeartbeatResponse();
response.setClusterId(request.getClusterId());
if (!this.isLeader) {
response.setError(Errors.NOT_LEADER);
closure.sendResponse(response);
return;
}
try {
// Only save the data
final StorePingEvent storePingEvent = new StorePingEvent(request, this.metadataStore);
final PipelineFuture