Search in sources :

Example 86 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class V1Decoder method decode.

public void decode(final LogEntry log, final byte[] content) {
    // 1-5 type
    final int iType = Bits.getInt(content, 1);
    log.setType(EnumOutter.EntryType.forNumber(iType));
    // 5-13 index
    // 13-21 term
    final long index = Bits.getLong(content, 5);
    final long term = Bits.getLong(content, 13);
    log.setId(new LogId(index, term));
    // 21-25 peer count
    int peerCount = Bits.getInt(content, 21);
    // peers
    int pos = 25;
    if (peerCount > 0) {
        List<PeerId> peers = new ArrayList<>(peerCount);
        while (peerCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            peers.add(peer);
        }
        log.setPeers(peers);
    }
    // old peers
    int oldPeerCount = Bits.getInt(content, pos);
    pos += 4;
    if (oldPeerCount > 0) {
        List<PeerId> oldPeers = new ArrayList<>(oldPeerCount);
        while (oldPeerCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            oldPeers.add(peer);
        }
        log.setOldPeers(oldPeers);
    }
    // data
    if (content.length > pos) {
        final int len = content.length - pos;
        ByteBuffer data = ByteBuffer.allocate(len);
        data.put(content, pos, len);
        data.flip();
        log.setData(data);
    }
}
Also used : ArrayList(java.util.ArrayList) LogId(com.alipay.sofa.jraft.entity.LogId) ByteBuffer(java.nio.ByteBuffer) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 87 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId 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 88 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class CounterClient method main.

public static void main(final String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Usage : java com.alipay.sofa.jraft.example.counter.CounterClient {groupId} {conf}");
        System.out.println("Example: java com.alipay.sofa.jraft.example.counter.CounterClient counter 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
        System.exit(1);
    }
    final String groupId = args[0];
    final String confStr = args[1];
    CounterGrpcHelper.initGRpc();
    final Configuration conf = new Configuration();
    if (!conf.parse(confStr)) {
        throw new IllegalArgumentException("Fail to parse conf:" + confStr);
    }
    RouteTable.getInstance().updateConfiguration(groupId, conf);
    final CliClientServiceImpl cliClientService = new CliClientServiceImpl();
    cliClientService.init(new CliOptions());
    if (!RouteTable.getInstance().refreshLeader(cliClientService, groupId, 1000).isOk()) {
        throw new IllegalStateException("Refresh leader failed");
    }
    final PeerId leader = RouteTable.getInstance().selectLeader(groupId);
    System.out.println("Leader is " + leader);
    final int n = 1000;
    final CountDownLatch latch = new CountDownLatch(n);
    final long start = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        incrementAndGet(cliClientService, leader, i, latch);
    }
    latch.await();
    System.out.println(n + " ops, cost : " + (System.currentTimeMillis() - start) + " ms.");
    System.exit(0);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 89 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class RouteTableTest method testRefreshLeaderWhenLeaderStops.

@Test
public void testRefreshLeaderWhenLeaderStops() throws Exception {
    final RouteTable rt = RouteTable.getInstance();
    testUpdateConfSelectLeader();
    PeerId leader = rt.selectLeader(groupId);
    this.cluster.stop(leader.getEndpoint());
    this.cluster.waitLeader();
    final PeerId oldLeader = leader.copy();
    assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
    leader = rt.selectLeader(groupId);
    assertNotEquals(leader, oldLeader);
    assertEquals(leader, cluster.getLeader().getNodeId().getPeerId());
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 90 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class RouteTableTest method testUpdateLeaderNull.

@Test
public void testUpdateLeaderNull() throws Exception {
    this.testUpdateConfSelectLeader();
    final RouteTable rt = RouteTable.getInstance();
    rt.updateLeader(groupId, (PeerId) null);
    assertNull(rt.selectLeader(groupId));
    assertTrue(rt.refreshLeader(cliClientService, groupId, 10000).isOk());
    final PeerId leader = rt.selectLeader(groupId);
    assertEquals(leader, cluster.getLeader().getNodeId().getPeerId());
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8