Search in sources :

Example 86 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project controller by opendaylight.

the class LeaderTest method testSendSnapshotfromInstallSnapshotReply.

@Test
public void testSendSnapshotfromInstallSnapshotReply() throws Exception {
    logStart("testSendSnapshotfromInstallSnapshotReply");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    final int commitIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl() {

        @Override
        public int getSnapshotChunkSize() {
            return 50;
        }
    };
    configParams.setHeartBeatInterval(new FiniteDuration(9, TimeUnit.SECONDS));
    configParams.setIsolatedLeaderCheckInterval(new FiniteDuration(10, TimeUnit.SECONDS));
    actorContext.setConfigParams(configParams);
    actorContext.setCommitIndex(commitIndex);
    leader = new Leader(actorContext);
    actorContext.setCurrentBehavior(leader);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    ByteString bs = toByteString(leadersSnapshot);
    Snapshot snapshot = Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null);
    leader.handleMessage(leaderActor, new SendInstallSnapshot(snapshot, ByteSource.wrap(bs.toByteArray())));
    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(1, installSnapshot.getChunkIndex());
    assertEquals(3, installSnapshot.getTotalChunks());
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
    installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    assertEquals(2, installSnapshot.getChunkIndex());
    assertEquals(3, installSnapshot.getTotalChunks());
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
    installSnapshot = MessageCollectorActor.expectFirstMatching(followerActor, InstallSnapshot.class);
    // Send snapshot reply one more time and make sure that a new snapshot message should not be sent to follower
    followerActor.underlyingActor().clear();
    leader.handleMessage(followerActor, new InstallSnapshotReply(actorContext.getTermInformation().getCurrentTerm(), FOLLOWER_ID, installSnapshot.getChunkIndex(), true));
    installSnapshot = MessageCollectorActor.getFirstMatching(followerActor, InstallSnapshot.class);
    assertNull(installSnapshot);
}
Also used : SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) HashMap(java.util.HashMap) InstallSnapshotReply(org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) FiniteDuration(scala.concurrent.duration.FiniteDuration) DefaultConfigParamsImpl(org.opendaylight.controller.cluster.raft.DefaultConfigParamsImpl) ByteString(com.google.protobuf.ByteString) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Snapshot(org.opendaylight.controller.cluster.raft.persisted.Snapshot) InstallSnapshot(org.opendaylight.controller.cluster.raft.messages.InstallSnapshot) CaptureSnapshot(org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot) SendInstallSnapshot(org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot) Test(org.junit.Test)

Example 87 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project controller by opendaylight.

the class LeaderTest method testHandleInstallSnapshotReplyLastChunk.

@Test
public void testHandleInstallSnapshotReplyLastChunk() throws Exception {
    logStart("testHandleInstallSnapshotReplyLastChunk");
    MockRaftActorContext actorContext = createActorContextWithFollower();
    final int commitIndex = 3;
    final int snapshotIndex = 2;
    final int snapshotTerm = 1;
    final int currentTerm = 2;
    actorContext.setCommitIndex(commitIndex);
    leader = new Leader(actorContext);
    actorContext.setCurrentBehavior(leader);
    leader.getFollower(FOLLOWER_ID).setMatchIndex(-1);
    leader.getFollower(FOLLOWER_ID).setNextIndex(0);
    // Ignore initial heartbeat.
    MessageCollectorActor.expectFirstMatching(followerActor, AppendEntries.class);
    Map<String, String> leadersSnapshot = new HashMap<>();
    leadersSnapshot.put("1", "A");
    leadersSnapshot.put("2", "B");
    leadersSnapshot.put("3", "C");
    // set the snapshot variables in replicatedlog
    actorContext.getReplicatedLog().setSnapshotIndex(snapshotIndex);
    actorContext.getReplicatedLog().setSnapshotTerm(snapshotTerm);
    actorContext.getTermInformation().update(currentTerm, leaderActor.path().toString());
    ByteString bs = toByteString(leadersSnapshot);
    leader.setSnapshotHolder(new SnapshotHolder(Snapshot.create(ByteState.of(bs.toByteArray()), Collections.<ReplicatedLogEntry>emptyList(), commitIndex, snapshotTerm, commitIndex, snapshotTerm, -1, null, null), ByteSource.wrap(bs.toByteArray())));
    LeaderInstallSnapshotState fts = new LeaderInstallSnapshotState(actorContext.getConfigParams().getSnapshotChunkSize(), leader.logName());
    fts.setSnapshotBytes(ByteSource.wrap(bs.toByteArray()));
    leader.getFollower(FOLLOWER_ID).setLeaderInstallSnapshotState(fts);
    while (!fts.isLastChunk(fts.getChunkIndex())) {
        fts.getNextChunk();
        fts.incrementChunkIndex();
    }
    // clears leaders log
    actorContext.getReplicatedLog().removeFrom(0);
    RaftActorBehavior raftBehavior = leader.handleMessage(followerActor, new InstallSnapshotReply(currentTerm, FOLLOWER_ID, fts.getChunkIndex(), true));
    assertTrue(raftBehavior instanceof Leader);
    assertEquals(1, leader.followerLogSize());
    FollowerLogInformation fli = leader.getFollower(FOLLOWER_ID);
    assertNotNull(fli);
    assertNull(fli.getInstallSnapshotState());
    assertEquals(commitIndex, fli.getMatchIndex());
    assertEquals(commitIndex + 1, fli.getNextIndex());
    assertFalse(leader.hasSnapshot());
}
Also used : FollowerLogInformation(org.opendaylight.controller.cluster.raft.FollowerLogInformation) HashMap(java.util.HashMap) InstallSnapshotReply(org.opendaylight.controller.cluster.raft.messages.InstallSnapshotReply) ByteString(com.google.protobuf.ByteString) MockRaftActorContext(org.opendaylight.controller.cluster.raft.MockRaftActorContext) ByteString(com.google.protobuf.ByteString) SnapshotHolder(org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader.SnapshotHolder) Test(org.junit.Test)

Example 88 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project incubator-pulsar by apache.

the class MessageCrypto method decryptData.

private ByteBuf decryptData(SecretKey dataKeySecret, MessageMetadata msgMetadata, ByteBuf payload) {
    // unpack iv and encrypted data
    ByteString ivString = msgMetadata.getEncryptionParam();
    ivString.copyTo(iv, 0);
    GCMParameterSpec gcmParams = new GCMParameterSpec(tagLen, iv);
    ByteBuf targetBuf = null;
    try {
        cipher.init(Cipher.DECRYPT_MODE, dataKeySecret, gcmParams);
        ByteBuffer sourceNioBuf = payload.nioBuffer(payload.readerIndex(), payload.readableBytes());
        int maxLength = cipher.getOutputSize(payload.readableBytes());
        targetBuf = PooledByteBufAllocator.DEFAULT.buffer(maxLength, maxLength);
        ByteBuffer targetNioBuf = targetBuf.nioBuffer(0, maxLength);
        int decryptedSize = cipher.doFinal(sourceNioBuf, targetNioBuf);
        targetBuf.writerIndex(decryptedSize);
    } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | ShortBufferException e) {
        log.error("{} Failed to decrypt message {}", logCtx, e.getMessage());
        if (targetBuf != null) {
            targetBuf.release();
            targetBuf = null;
        }
    }
    return targetBuf;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ByteString(com.google.protobuf.ByteString) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ShortBufferException(javax.crypto.ShortBufferException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) BadPaddingException(javax.crypto.BadPaddingException) ByteBuf(io.netty.buffer.ByteBuf) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer)

Example 89 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project incubator-pulsar by apache.

the class ByteBufCodedInputStream method readBytes.

/**
 * Read a {@code bytes} field value from the stream.
 */
public ByteString readBytes() throws IOException {
    final int size = readRawVarint32();
    if (size == 0) {
        return ByteString.EMPTY;
    } else {
        byte[] localBuf = localByteArray.get();
        if (localBuf == null || localBuf.length < size) {
            localBuf = new byte[Math.max(size, 1024)];
            localByteArray.set(localBuf);
        }
        buf.readBytes(localBuf, 0, size);
        ByteString res = ByteString.copyFrom(localBuf, 0, size);
        return res;
    }
}
Also used : ByteString(com.google.protobuf.ByteString)

Example 90 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project jabci by jTendermint.

the class JavaCounter method requestCheckTx.

@Override
public ResponseCheckTx requestCheckTx(RequestCheckTx req) {
    System.out.println("got check tx");
    ByteString tx = req.getTx();
    if (tx.size() <= 4) {
        // hopefully parsable integer
        int txCheck = new BigInteger(1, tx.toByteArray()).intValueExact();
        System.out.println("tx value is: " + txCheck);
        if (txCheck < txCount) {
            String err = "Invalid nonce. Expected >= " + txCount + ", got " + txCheck;
            System.out.println(err);
            return ResponseCheckTx.newBuilder().setCode(CodeType.BadNonce).setLog(err).build();
        }
    }
    System.out.println("SENDING OK");
    return ResponseCheckTx.newBuilder().setCode(CodeType.OK).build();
}
Also used : ByteString(com.google.protobuf.ByteString) BigInteger(java.math.BigInteger) ByteString(com.google.protobuf.ByteString)

Aggregations

ByteString (com.google.protobuf.ByteString)407 Test (org.junit.Test)143 ArrayList (java.util.ArrayList)65 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)63 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)40 HashMap (java.util.HashMap)40 IOException (java.io.IOException)38 List (java.util.List)34 Map (java.util.Map)33 ServerRequest (com.pokegoapi.main.ServerRequest)17 ExecutionException (java.util.concurrent.ExecutionException)16 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)15 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)15 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)15 Feature (com.google.cloud.vision.v1.Feature)15 Image (com.google.cloud.vision.v1.Image)15 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)15 ByteBuffer (java.nio.ByteBuffer)14 FileInputStream (java.io.FileInputStream)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)12