Example usage for org.apache.commons.lang3 SerializationUtils serialize

List of usage examples for org.apache.commons.lang3 SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SerializationUtils serialize.

Prototype

public static byte[] serialize(final Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:org.opendaylight.controller.cluster.raft.behaviors.SnapshotTrackerTest.java

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);

    data = new HashMap<>();
    data.put("key1", "value1");
    data.put("key2", "value2");
    data.put("key3", "value3");

    byteString = ByteString.copyFrom(SerializationUtils.serialize((Serializable) data));
    chunk1 = getNextChunk(byteString, 0, 10);
    chunk2 = getNextChunk(byteString, 10, 10);
    chunk3 = getNextChunk(byteString, 20, byteString.size());

    fbos = spy(new FileBackedOutputStream(100000000, "target"));
    FileBackedOutputStreamFactory mockFactory = mock(FileBackedOutputStreamFactory.class);
    doReturn(fbos).when(mockFactory).newInstance();
    doReturn(mockFactory).when(mockContext).getFileBackedOutputStreamFactory();
}

From source file:org.opendaylight.controller.cluster.raft.GetSnapshotReplyActor.java

@Override
public void onReceive(Object message) {
    if (message instanceof CaptureSnapshotReply) {
        Snapshot snapshot = Snapshot.create(((CaptureSnapshotReply) message).getSnapshot(),
                params.captureSnapshot.getUnAppliedEntries(), params.captureSnapshot.getLastIndex(),
                params.captureSnapshot.getLastTerm(), params.captureSnapshot.getLastAppliedIndex(),
                params.captureSnapshot.getLastAppliedTerm(), params.electionTerm.getCurrentTerm(),
                params.electionTerm.getVotedFor(), params.peerInformation);

        LOG.debug("{}: Received CaptureSnapshotReply, sending {}", params.id, snapshot);

        params.replyToActor.tell(new GetSnapshotReply(params.id, SerializationUtils.serialize(snapshot)),
                getSelf());//from  w ww . ja  v  a  2s  .  c o  m
        getSelf().tell(PoisonPill.getInstance(), getSelf());
    } else if (message instanceof ReceiveTimeout) {
        LOG.warn("{}: Got ReceiveTimeout for inactivity - did not receive CaptureSnapshotReply within {} ms",
                params.id, params.receiveTimeout.toMillis());

        params.replyToActor.tell(new akka.actor.Status.Failure(new TimeoutException(
                String.format("Timed out after %d ms while waiting for CaptureSnapshotReply",
                        params.receiveTimeout.toMillis()))),
                getSelf());
        getSelf().tell(PoisonPill.getInstance(), getSelf());
    }
}

From source file:org.opendaylight.controller.cluster.raft.RaftActorSnapshotMessageSupport.java

private void onGetSnapshot(ActorRef sender) {
    log.debug("{}: onGetSnapshot", context.getId());

    if (context.getPersistenceProvider().isRecoveryApplicable()) {
        CaptureSnapshot captureSnapshot = context.getSnapshotManager()
                .newCaptureSnapshot(context.getReplicatedLog().last(), -1, false);

        ActorRef snapshotReplyActor = context.actorOf(GetSnapshotReplyActor.props(captureSnapshot,
                ImmutableElectionTerm.copyOf(context.getTermInformation()), sender, snapshotReplyActorTimeout,
                context.getId(), context.getPeerServerInfo(true)));

        cohort.createSnapshot(snapshotReplyActor);
    } else {/*ww  w .  j  av  a  2 s . c o  m*/
        Snapshot snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1,
                -1, -1, context.getTermInformation().getCurrentTerm(),
                context.getTermInformation().getVotedFor(), context.getPeerServerInfo(true));

        sender.tell(new GetSnapshotReply(context.getId(), SerializationUtils.serialize(snapshot)),
                context.getActor());
    }
}

From source file:org.opendaylight.controller.cluster.raft.RaftActorTest.java

@Test
public void testRestoreFromSnapshot() throws Exception {
    TEST_LOG.info("testRestoreFromSnapshot starting");

    String persistenceId = factory.generateActorId("test-actor-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());

    List<ReplicatedLogEntry> snapshotUnappliedEntries = new ArrayList<>();
    snapshotUnappliedEntries.add(/*ww w  .  j  a v a2  s.  c  o  m*/
            new MockRaftActorContext.MockReplicatedLogEntry(1, 4, new MockRaftActorContext.MockPayload("E")));

    int snapshotLastApplied = 3;
    int snapshotLastIndex = 4;

    List<MockPayload> state = Arrays.asList(new MockRaftActorContext.MockPayload("A"),
            new MockRaftActorContext.MockPayload("B"), new MockRaftActorContext.MockPayload("C"),
            new MockRaftActorContext.MockPayload("D"));
    ByteString stateBytes = fromObject(state);

    Snapshot snapshot = Snapshot.create(stateBytes.toByteArray(), snapshotUnappliedEntries, snapshotLastIndex,
            1, snapshotLastApplied, 1, 1, "member-1");

    InMemorySnapshotStore.addSnapshotSavedLatch(persistenceId);

    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId)
            .config(config).restoreFromSnapshot(SerializationUtils.serialize(snapshot)).props()
            .withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();

    mockRaftActor.waitForRecoveryComplete();

    Snapshot savedSnapshot = InMemorySnapshotStore.waitForSavedSnapshot(persistenceId, Snapshot.class);
    assertEquals("getElectionTerm", snapshot.getElectionTerm(), savedSnapshot.getElectionTerm());
    assertEquals("getElectionVotedFor", snapshot.getElectionVotedFor(), savedSnapshot.getElectionVotedFor());
    assertEquals("getLastAppliedIndex", snapshot.getLastAppliedIndex(), savedSnapshot.getLastAppliedIndex());
    assertEquals("getLastAppliedTerm", snapshot.getLastAppliedTerm(), savedSnapshot.getLastAppliedTerm());
    assertEquals("getLastIndex", snapshot.getLastIndex(), savedSnapshot.getLastIndex());
    assertEquals("getLastTerm", snapshot.getLastTerm(), savedSnapshot.getLastTerm());
    assertArrayEquals("getState", snapshot.getState(), savedSnapshot.getState());
    assertEquals("getUnAppliedEntries", snapshot.getUnAppliedEntries(), savedSnapshot.getUnAppliedEntries());

    verify(mockRaftActor.snapshotCohortDelegate, timeout(5000)).applySnapshot(any(byte[].class));

    RaftActorContext context = mockRaftActor.getRaftActorContext();
    assertEquals("Journal log size", 1, context.getReplicatedLog().size());
    assertEquals("Last index", snapshotLastIndex, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", snapshotLastApplied, context.getLastApplied());
    assertEquals("Commit index", snapshotLastApplied, context.getCommitIndex());
    assertEquals("Recovered state", state, mockRaftActor.getState());
    assertEquals("Current term", 1L, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted for", "member-1", context.getTermInformation().getVotedFor());

    // Test with data persistence disabled

    snapshot = Snapshot.create(new byte[0], Collections.<ReplicatedLogEntry>emptyList(), -1, -1, -1, -1, 5,
            "member-1");

    persistenceId = factory.generateActorId("test-actor-");

    raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId).config(config)
            .restoreFromSnapshot(SerializationUtils.serialize(snapshot)).persistent(Optional.of(Boolean.FALSE))
            .props().withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    mockRaftActor = raftActorRef.underlyingActor();

    mockRaftActor.waitForRecoveryComplete();
    assertEquals("snapshot committed", true,
            Uninterruptibles.awaitUninterruptibly(mockRaftActor.snapshotCommitted, 5, TimeUnit.SECONDS));

    context = mockRaftActor.getRaftActorContext();
    assertEquals("Current term", 5L, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted for", "member-1", context.getTermInformation().getVotedFor());

    TEST_LOG.info("testRestoreFromSnapshot ending");
}

From source file:org.opendaylight.controller.cluster.raft.RaftActorTest.java

@Test
public void testRestoreFromSnapshotWithRecoveredData() throws Exception {
    TEST_LOG.info("testRestoreFromSnapshotWithRecoveredData starting");

    String persistenceId = factory.generateActorId("test-actor-");
    DefaultConfigParamsImpl config = new DefaultConfigParamsImpl();
    config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());

    List<MockPayload> state = Arrays.asList(new MockRaftActorContext.MockPayload("A"));
    Snapshot snapshot = Snapshot.create(fromObject(state).toByteArray(), Arrays.<ReplicatedLogEntry>asList(), 5,
            2, 5, 2, 2, "member-1");

    InMemoryJournal.addEntry(persistenceId, 1,
            new MockRaftActorContext.MockReplicatedLogEntry(1, 0, new MockRaftActorContext.MockPayload("B")));

    TestActorRef<MockRaftActor> raftActorRef = factory.createTestActor(MockRaftActor.builder().id(persistenceId)
            .config(config).restoreFromSnapshot(SerializationUtils.serialize(snapshot)).props()
            .withDispatcher(Dispatchers.DefaultDispatcherId()), persistenceId);
    MockRaftActor mockRaftActor = raftActorRef.underlyingActor();

    mockRaftActor.waitForRecoveryComplete();

    Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
    verify(mockRaftActor.snapshotCohortDelegate, never()).applySnapshot(any(byte[].class));

    RaftActorContext context = mockRaftActor.getRaftActorContext();
    assertEquals("Journal log size", 1, context.getReplicatedLog().size());
    assertEquals("Last index", 0, context.getReplicatedLog().lastIndex());
    assertEquals("Last applied", -1, context.getLastApplied());
    assertEquals("Commit index", -1, context.getCommitIndex());
    assertEquals("Current term", 0, context.getTermInformation().getCurrentTerm());
    assertEquals("Voted for", null, context.getTermInformation().getVotedFor());

    TEST_LOG.info("testRestoreFromSnapshotWithRecoveredData ending");
}

From source file:org.opendaylight.controller.cluster.raft.ReplicationAndSnapshotsWithLaggingFollowerIntegrationTest.java

/**
 * Resume the lagging follower 2 and verify it receives an install snapshot from the leader.
 *//*from ww w.j  av  a 2  s . c o m*/
private void verifyInstallSnapshotToLaggingFollower(long lastAppliedIndex,
        @Nullable ServerConfigurationPayload expServerConfig) throws Exception {
    testLog.info("verifyInstallSnapshotToLaggingFollower starting");

    MessageCollectorActor.clearMessages(leaderCollectorActor);

    // Now stop dropping AppendEntries in follower 2.
    follower2Actor.underlyingActor().stopDropMessages(AppendEntries.class);

    MessageCollectorActor.expectFirstMatching(leaderCollectorActor, SaveSnapshotSuccess.class);

    // Verify the leader's persisted snapshot. The previous snapshot (currently) won't be deleted from
    // the snapshot store because the second snapshot was initiated by the follower install snapshot and
    // not because the batch count was reached so the persisted journal sequence number wasn't advanced
    // far enough to cause the previous snapshot to be deleted. This is because
    // RaftActor#trimPersistentData subtracts the snapshotBatchCount from the snapshot's sequence number.
    // This is OK - the next snapshot should delete it. In production, even if the system restarted
    // before another snapshot, they would both get applied which wouldn't hurt anything.
    List<Snapshot> persistedSnapshots = InMemorySnapshotStore.getSnapshots(leaderId, Snapshot.class);
    Assert.assertTrue("Expected at least 1 persisted snapshots", persistedSnapshots.size() > 0);
    Snapshot persistedSnapshot = persistedSnapshots.get(persistedSnapshots.size() - 1);
    verifySnapshot("Persisted", persistedSnapshot, currentTerm, lastAppliedIndex, currentTerm,
            lastAppliedIndex);
    List<ReplicatedLogEntry> unAppliedEntry = persistedSnapshot.getUnAppliedEntries();
    assertEquals("Persisted Snapshot getUnAppliedEntries size", 0, unAppliedEntry.size());

    int snapshotSize = SerializationUtils.serialize(persistedSnapshot.getState()).length;
    final int expTotalChunks = snapshotSize / SNAPSHOT_CHUNK_SIZE
            + (snapshotSize % SNAPSHOT_CHUNK_SIZE > 0 ? 1 : 0);

    InstallSnapshot installSnapshot = MessageCollectorActor.expectFirstMatching(follower2CollectorActor,
            InstallSnapshot.class);
    assertEquals("InstallSnapshot getTerm", currentTerm, installSnapshot.getTerm());
    assertEquals("InstallSnapshot getLeaderId", leaderId, installSnapshot.getLeaderId());
    assertEquals("InstallSnapshot getChunkIndex", 1, installSnapshot.getChunkIndex());
    assertEquals("InstallSnapshot getTotalChunks", expTotalChunks, installSnapshot.getTotalChunks());
    assertEquals("InstallSnapshot getLastIncludedTerm", currentTerm, installSnapshot.getLastIncludedTerm());
    assertEquals("InstallSnapshot getLastIncludedIndex", lastAppliedIndex,
            installSnapshot.getLastIncludedIndex());
    //assertArrayEquals("InstallSnapshot getData", snapshot, installSnapshot.getData().toByteArray());

    List<InstallSnapshotReply> installSnapshotReplies = MessageCollectorActor
            .expectMatching(leaderCollectorActor, InstallSnapshotReply.class, expTotalChunks);
    int index = 1;
    for (InstallSnapshotReply installSnapshotReply : installSnapshotReplies) {
        assertEquals("InstallSnapshotReply getTerm", currentTerm, installSnapshotReply.getTerm());
        assertEquals("InstallSnapshotReply getChunkIndex", index++, installSnapshotReply.getChunkIndex());
        assertEquals("InstallSnapshotReply getFollowerId", follower2Id, installSnapshotReply.getFollowerId());
        assertEquals("InstallSnapshotReply isSuccess", true, installSnapshotReply.isSuccess());
    }

    // Verify follower 2 applies the snapshot.
    ApplySnapshot applySnapshot = MessageCollectorActor.expectFirstMatching(follower2CollectorActor,
            ApplySnapshot.class);
    verifySnapshot("Follower 2", applySnapshot.getSnapshot(), currentTerm, lastAppliedIndex, currentTerm,
            lastAppliedIndex);
    assertEquals("Persisted Snapshot getUnAppliedEntries size", 0,
            applySnapshot.getSnapshot().getUnAppliedEntries().size());

    // Wait for the snapshot to complete.
    MessageCollectorActor.expectFirstMatching(leaderCollectorActor, SaveSnapshotSuccess.class);

    // Ensure there's at least 1 more heartbeat.
    MessageCollectorActor.clearMessages(leaderCollectorActor);
    MessageCollectorActor.expectFirstMatching(leaderCollectorActor, AppendEntriesReply.class);

    // The leader should now have performed fake snapshots to advance the snapshot index and to trim
    // the log. In addition replicatedToAllIndex should've advanced.
    verifyLeadersTrimmedLog(lastAppliedIndex);

    if (expServerConfig != null) {
        Set<ServerInfo> expServerInfo = new HashSet<>(expServerConfig.getServerConfig());
        assertEquals("Leader snapshot server config", expServerInfo,
                new HashSet<>(persistedSnapshot.getServerConfiguration().getServerConfig()));

        assertEquals("Follower 2 snapshot server config", expServerInfo,
                new HashSet<>(applySnapshot.getSnapshot().getServerConfiguration().getServerConfig()));

        ServerConfigurationPayload follower2ServerConfig = follower2Context.getPeerServerInfo(true);
        assertNotNull("Follower 2 server config is null", follower2ServerConfig);

        assertEquals("Follower 2 server config", expServerInfo,
                new HashSet<>(follower2ServerConfig.getServerConfig()));
    }

    MessageCollectorActor.clearMessages(leaderCollectorActor);
    MessageCollectorActor.clearMessages(follower1CollectorActor);
    MessageCollectorActor.clearMessages(follower2CollectorActor);

    testLog.info("verifyInstallSnapshotToLaggingFollower complete");
}

From source file:org.optaplanner.core.impl.testdata.util.PlannerTestUtils.java

public static <T> T serializeAndDeserializeWithJavaSerialization(T input) {
    byte[] bytes = SerializationUtils.serialize((Serializable) input);
    return (T) SerializationUtils.deserialize(bytes);
}

From source file:org.ow2.proactive.scheduler.core.db.JobContent.java

public void setInitJobContent(Job job) {
    byte[] jobByte = SerializationUtils.serialize(job);
    try {/* ww w . j a va2 s  .c o  m*/
        this.jobContentAsByteArray = ByteCompressionUtils.compress(jobByte);
    } catch (Exception e) {
        LOGGER.error(e);
        this.jobContentAsByteArray = jobByte;
    }
}

From source file:org.ow2.proactive.scheduler.util.ByteCompressionUtilsTest.java

@Before
public void setup() throws UserException {
    job = new TaskFlowJob();
    job.setName(this.getClass().getName());
    job.addTask(new JavaTask());

    jobByte = SerializationUtils.serialize(job);
}

From source file:org.pascani.dsl.lib.infrastructure.NamespaceProxy.java

/**
 * Performs an RPC call to a remote namespace
 * //from   w  ww  .j  a v  a2s  .c  om
 * @param message
 *            The payload of the message
 * @param defaultValue
 *            A decent value to nicely return in case an {@link Exception}
 *            is thrown
 * @return The response from the RPC server (i.e., a remote component
 *         processing RPC requests) configured with the routing key of the
 *         {@link RpcClient} instance
 */
private byte[] makeActualCall(RpcRequest request, Serializable defaultValue) {
    byte[] message = SerializationUtils.serialize(request);
    byte[] response = SerializationUtils.serialize(defaultValue);
    try {
        response = client.makeRequest(message);
    } catch (Exception e) {
        this.logger.error("Error performing an RPC call to namespace " + this.client.routingKey(),
                e.getCause());
        throw new RuntimeException(e);
    }
    return response;
}