Search in sources :

Example 31 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class InstructionProcessor method processTransferLeader.

private boolean processTransferLeader(final Instruction instruction) {
    try {
        final Instruction.TransferLeader transferLeader = instruction.getTransferLeader();
        if (transferLeader == null) {
            return false;
        }
        final Endpoint toEndpoint = transferLeader.getMoveToEndpoint();
        if (toEndpoint == null) {
            LOG.error("TransferLeader#toEndpoint must not be null, {}.", instruction);
            return false;
        }
        final Region region = instruction.getRegion();
        final long regionId = region.getId();
        final RegionEngine engine = this.storeEngine.getRegionEngine(regionId);
        if (engine == null) {
            LOG.error("Could not found regionEngine, {}.", instruction);
            return false;
        }
        if (!region.equals(engine.getRegion())) {
            LOG.warn("Instruction [{}] is out of date.", instruction);
            return false;
        }
        return engine.transferLeadershipTo(toEndpoint);
    } catch (final Throwable t) {
        LOG.error("Caught an exception on #processTransferLeader: {}.", StackTraceUtil.stackTrace(t));
        return false;
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngine(com.alipay.sofa.jraft.rhea.RegionEngine) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction)

Example 32 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class MetadataRpcClient method internalCreateRegionId.

private void internalCreateRegionId(final long clusterId, final Endpoint endpoint, final CompletableFuture<Long> future, final int retriesLeft, final Errors lastCause) {
    final RetryRunner retryRunner = retryCause -> internalCreateRegionId(clusterId, endpoint, future, retriesLeft - 1, retryCause);
    final FailoverClosure<Long> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
    final CreateRegionIdRequest request = new CreateRegionIdRequest();
    request.setClusterId(clusterId);
    request.setEndpoint(endpoint);
    this.pdRpcService.callPdServerWithRpc(request, closure, lastCause);
}
Also used : Store(com.alipay.sofa.jraft.rhea.metadata.Store) RetryRunner(com.alipay.sofa.jraft.rhea.client.failover.RetryRunner) FailoverClosure(com.alipay.sofa.jraft.rhea.client.failover.FailoverClosure) CreateRegionIdRequest(com.alipay.sofa.jraft.rhea.cmd.pd.CreateRegionIdRequest) CompletableFuture(java.util.concurrent.CompletableFuture) GetStoreInfoRequest(com.alipay.sofa.jraft.rhea.cmd.pd.GetStoreInfoRequest) Errors(com.alipay.sofa.jraft.rhea.errors.Errors) Cluster(com.alipay.sofa.jraft.rhea.metadata.Cluster) GetStoreIdRequest(com.alipay.sofa.jraft.rhea.cmd.pd.GetStoreIdRequest) SetStoreInfoRequest(com.alipay.sofa.jraft.rhea.cmd.pd.SetStoreInfoRequest) GetClusterInfoRequest(com.alipay.sofa.jraft.rhea.cmd.pd.GetClusterInfoRequest) Endpoint(com.alipay.sofa.jraft.util.Endpoint) FutureHelper(com.alipay.sofa.jraft.rhea.client.FutureHelper) FailoverClosureImpl(com.alipay.sofa.jraft.rhea.client.failover.impl.FailoverClosureImpl) FailoverClosureImpl(com.alipay.sofa.jraft.rhea.client.failover.impl.FailoverClosureImpl) CreateRegionIdRequest(com.alipay.sofa.jraft.rhea.cmd.pd.CreateRegionIdRequest) RetryRunner(com.alipay.sofa.jraft.rhea.client.failover.RetryRunner)

Example 33 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class PeerId method parse.

/**
 * Parse peerId from string that generated by {@link #toString()}
 * This method can support parameter string values are below:
 *
 * <pre>
 * PeerId.parse("a:b")          = new PeerId("a", "b", 0 , -1)
 * PeerId.parse("a:b:c")        = new PeerId("a", "b", "c", -1)
 * PeerId.parse("a:b::d")       = new PeerId("a", "b", 0, "d")
 * PeerId.parse("a:b:c:d")      = new PeerId("a", "b", "c", "d")
 * </pre>
 */
public boolean parse(final String s) {
    if (StringUtils.isEmpty(s)) {
        return false;
    }
    final String[] tmps = Utils.parsePeerId(s);
    if (tmps.length < 2 || tmps.length > 4) {
        return false;
    }
    try {
        final int port = Integer.parseInt(tmps[1]);
        this.endpoint = new Endpoint(tmps[0], port);
        switch(tmps.length) {
            case 3:
                this.idx = Integer.parseInt(tmps[2]);
                break;
            case 4:
                if (tmps[2].equals("")) {
                    this.idx = 0;
                } else {
                    this.idx = Integer.parseInt(tmps[2]);
                }
                this.priority = Integer.parseInt(tmps[3]);
                break;
            default:
                break;
        }
        this.str = null;
        return true;
    } catch (final Exception e) {
        LOG.error("Parse peer from string failed: {}.", s, e);
        return false;
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 34 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class BenchmarkClient method rebalance.

// Because we use fake PD, so we need manual rebalance
public static void rebalance(final RheaKVStore rheaKVStore, final String initialServerList, final List<RegionRouteTableOptions> regionRouteTableOptionsList) {
    final PlacementDriverClient pdClient = rheaKVStore.getPlacementDriverClient();
    final Configuration configuration = new Configuration();
    configuration.parse(initialServerList);
    final int serverSize = configuration.size();
    final int regionSize = regionRouteTableOptionsList.size();
    final int regionSizePerServer = regionSize / serverSize;
    final Queue<Long> regions = new ArrayDeque<>();
    for (final RegionRouteTableOptions r : regionRouteTableOptionsList) {
        regions.add(r.getRegionId());
    }
    final Map<PeerId, Integer> peerMap = Maps.newHashMap();
    for (; ; ) {
        final Long regionId = regions.poll();
        if (regionId == null) {
            break;
        }
        PeerId peerId;
        try {
            final Endpoint endpoint = pdClient.getLeader(regionId, true, 10000);
            if (endpoint == null) {
                continue;
            }
            peerId = new PeerId(endpoint, 0);
            LOG.info("Region {} leader is {}", regionId, peerId);
        } catch (final Exception e) {
            regions.add(regionId);
            continue;
        }
        final Integer size = peerMap.get(peerId);
        if (size == null) {
            peerMap.put(peerId, 1);
            continue;
        }
        if (size < regionSizePerServer) {
            peerMap.put(peerId, size + 1);
            continue;
        }
        for (final PeerId p : configuration.listPeers()) {
            final Integer pSize = peerMap.get(p);
            if (pSize != null && pSize >= regionSizePerServer) {
                continue;
            }
            try {
                pdClient.transferLeader(regionId, JRaftHelper.toPeer(p), true);
                LOG.info("Region {} transfer leader to {}", regionId, p);
                regions.add(regionId);
                break;
            } catch (final Exception e) {
                LOG.error("Fail to transfer leader to {}", p);
            }
        }
    }
    for (final RegionRouteTableOptions r : regionRouteTableOptionsList) {
        final Long regionId = r.getRegionId();
        try {
            final Endpoint endpoint = pdClient.getLeader(regionId, true, 10000);
            LOG.info("Finally, the region: {} leader is: {}", regionId, endpoint);
        } catch (final Exception e) {
            LOG.error("Fail to get leader: {}", StackTraceUtil.stackTrace(e));
        }
    }
}
Also used : RegionRouteTableOptions(com.alipay.sofa.jraft.rhea.options.RegionRouteTableOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) Endpoint(com.alipay.sofa.jraft.util.Endpoint) ArrayDeque(java.util.ArrayDeque) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 35 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class Server2 method main.

public static void main(final String[] args) throws Exception {
    final PlacementDriverOptions pdOpts = PlacementDriverOptionsConfigured.newConfigured().withFake(// use a fake pd
    true).config();
    final StoreEngineOptions storeOpts = // 
    StoreEngineOptionsConfigured.newConfigured().withStorageType(StorageType.RocksDB).withRocksDBOptions(RocksDBOptionsConfigured.newConfigured().withDbPath(Configs.DB_PATH).config()).withRaftDataPath(Configs.RAFT_DATA_PATH).withServerAddress(new Endpoint("127.0.0.1", 8182)).config();
    final RheaKVStoreOptions opts = // 
    RheaKVStoreOptionsConfigured.newConfigured().withClusterName(// 
    Configs.CLUSTER_NAME).withUseParallelCompress(// 
    true).withInitialServerList(Configs.ALL_NODE_ADDRESSES).withStoreEngineOptions(// 
    storeOpts).withPlacementDriverOptions(// 
    pdOpts).config();
    System.out.println(opts);
    final Node node = new Node(opts);
    node.start();
    Runtime.getRuntime().addShutdownHook(new Thread(node::stop));
    System.out.println("server2 start OK");
}
Also used : RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions)

Aggregations

Endpoint (com.alipay.sofa.jraft.util.Endpoint)80 Test (org.junit.Test)34 PeerId (com.alipay.sofa.jraft.entity.PeerId)28 Node (com.alipay.sofa.jraft.Node)23 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 Configuration (com.alipay.sofa.jraft.conf.Configuration)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Status (com.alipay.sofa.jraft.Status)8 File (java.io.File)7 ByteBuffer (java.nio.ByteBuffer)7 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)5 Message (com.google.protobuf.Message)5 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)4