Search in sources :

Example 16 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class Zab1_0Test method testInitialAcceptedCurrent.

@Test
public void testInitialAcceptedCurrent() throws Exception {
    File tmpDir = File.createTempFile("test", ".dir", testData);
    tmpDir.delete();
    tmpDir.mkdir();
    try {
        FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
        File version2 = new File(tmpDir, "version-2");
        version2.mkdir();
        logFactory.save(new DataTree(), new ConcurrentHashMap<Long, Integer>(), false);
        long zxid = ZxidUtils.makeZxid(3, 3);
        logFactory.append(new Request(1, 1, ZooDefs.OpCode.error, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error), new ErrorTxn(1), zxid));
        logFactory.commit();
        ZKDatabase zkDb = new ZKDatabase(logFactory);
        QuorumPeer peer = QuorumPeer.testingQuorumPeer();
        peer.setZKDatabase(zkDb);
        peer.setTxnFactory(logFactory);
        peer.getLastLoggedZxid();
        assertEquals(3, peer.getAcceptedEpoch());
        assertEquals(3, peer.getCurrentEpoch());
        assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME))));
        assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME))));
    } finally {
        TestUtils.deleteFileRecursively(tmpDir);
    }
}
Also used : ErrorTxn(org.apache.zookeeper.txn.ErrorTxn) DataTree(org.apache.zookeeper.server.DataTree) Request(org.apache.zookeeper.server.Request) ZabUtils.createQuorumPeer(org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer) File(java.io.File) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 17 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class SerializeUtilsTest method testSerializeRequestWithoutTxn.

@Test
public void testSerializeRequestWithoutTxn() throws IOException {
    // Arrange
    TxnHeader header = mock(TxnHeader.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            OutputArchive oa = (OutputArchive) args[0];
            oa.writeString("header", "test");
            return null;
        }
    }).when(header).serialize(any(OutputArchive.class), anyString());
    Request request = new Request(1, 2, 3, header, null, 4);
    // Act
    byte[] data = SerializeUtils.serializeRequest(request);
    // Assert
    assertNotNull(data);
    verify(header).serialize(any(OutputArchive.class), eq("hdr"));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
    boa.writeString("header", "test");
    baos.close();
    assertArrayEquals(baos.toByteArray(), data);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) OutputArchive(org.apache.jute.OutputArchive) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Request(org.apache.zookeeper.server.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.jupiter.api.Test)

Example 18 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class SerializeUtilsTest method testSerializeRequestRequestHeaderIsNull.

@Test
public void testSerializeRequestRequestHeaderIsNull() {
    Request request = new Request(0, 0, 0, null, null, 0);
    byte[] data = SerializeUtils.serializeRequest(request);
    assertNull(data);
}
Also used : Request(org.apache.zookeeper.server.Request) Test(org.junit.jupiter.api.Test)

Example 19 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method noCrashOnCommittedRequestsOfUnseenRequestTest.

/**
 * In the following test, we verify that we can handle the case that we got a commit
 * of a request we never seen since the session that we just established. This can happen
 * when a session is just established and there is request waiting to be committed in the
 * session queue but it sees a commit for a request that belongs to the previous connection.
 */
@Test
@Timeout(value = 5)
public void noCrashOnCommittedRequestsOfUnseenRequestTest() throws Exception {
    final String path = "/noCrash/OnCommittedRequests/OfUnseenRequestTest";
    final int numberofReads = 10;
    final int sessionid = 0x123456;
    final int firstCXid = 0x100;
    int readReqId = firstCXid;
    processor.stoppedMainLoop = true;
    HashSet<Request> localRequests = new HashSet<Request>();
    // queue the blocking write request to queuedRequests
    Request firstCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, readReqId++);
    processor.queuedRequests.add(firstCommittedReq);
    processor.queuedWriteRequests.add(firstCommittedReq);
    localRequests.add(firstCommittedReq);
    // queue read requests to queuedRequests
    for (; readReqId <= numberofReads + firstCXid; ++readReqId) {
        Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionid, readReqId);
        processor.queuedRequests.add(readReq);
        localRequests.add(readReq);
    }
    // run once
    assertTrue(processor.queuedRequests.containsAll(localRequests));
    processor.initThreads(defaultSizeOfThreadPool);
    processor.run();
    Thread.sleep(1000);
    // We verify that the processor is waiting for the commit
    assertTrue(processedRequests.isEmpty());
    // We add a commit that belongs to the same session but with smaller cxid,
    // i.e., commit of an update from previous connection of this session.
    Request preSessionCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, firstCXid - 2);
    processor.committedRequests.add(preSessionCommittedReq);
    processor.committedRequests.add(firstCommittedReq);
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor processed the old commit prior to the newer messages
    assertTrue(processedRequests.peek() == preSessionCommittedReq);
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor handle all messages.
    assertTrue(processedRequests.containsAll(localRequests));
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 20 with Request

use of org.apache.zookeeper.server.Request in project zookeeper by apache.

the class CommitProcessorConcurrencyTest method noCrashOnOutofOrderCommittedRequestTest.

/**
 * In the following test, we verify if we handle the case in which we get a commit
 * for a request that has higher Cxid than the one we are waiting. This can happen
 * when a session connection is lost but there is a request waiting to be committed in the
 * session queue. However, since the session has moved, new requests can get to
 * the leader out of order. Hence, the commits can also arrive "out of order" w.r.t. cxid.
 * We should commit the requests according to the order we receive from the leader, i.e., wait for the relevant commit.
 */
@Test
@Timeout(value = 5)
public void noCrashOnOutofOrderCommittedRequestTest() throws Exception {
    final String path = "/noCrash/OnCommittedRequests/OfUnSeenRequestTest";
    final int sessionid = 0x123456;
    final int lastCXid = 0x100;
    final int numberofReads = 10;
    int readReqId = lastCXid;
    processor.stoppedMainLoop = true;
    HashSet<Request> localRequests = new HashSet<Request>();
    // queue the blocking write request to queuedRequests
    Request orphanCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, lastCXid);
    processor.queuedRequests.add(orphanCommittedReq);
    processor.queuedWriteRequests.add(orphanCommittedReq);
    localRequests.add(orphanCommittedReq);
    // queue read requests to queuedRequests
    for (; readReqId <= numberofReads + lastCXid; ++readReqId) {
        Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionid, readReqId);
        processor.queuedRequests.add(readReq);
        localRequests.add(readReq);
    }
    // run once
    processor.initThreads(defaultSizeOfThreadPool);
    processor.run();
    Thread.sleep(1000);
    // We verify that the processor is waiting for the commit
    assertTrue(processedRequests.isEmpty());
    // We add a commit that belongs to the same session but with larger cxid,
    // i.e., commit of an update from the next connection of this session.
    Request otherSessionCommittedReq = newRequest(new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), OpCode.create, sessionid, lastCXid + 10);
    processor.committedRequests.add(otherSessionCommittedReq);
    processor.committedRequests.add(orphanCommittedReq);
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor processed the old commit prior to the newer messages
    assertTrue(processedRequests.size() == 1);
    assertTrue(processedRequests.contains(otherSessionCommittedReq));
    processor.run();
    Thread.sleep(1000);
    // We verify that the commit processor handle all messages.
    assertTrue(processedRequests.containsAll(localRequests));
}
Also used : CreateRequest(org.apache.zookeeper.proto.CreateRequest) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) CreateRequest(org.apache.zookeeper.proto.CreateRequest) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) Request(org.apache.zookeeper.server.Request) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

Request (org.apache.zookeeper.server.Request)51 Test (org.junit.jupiter.api.Test)25 CreateRequest (org.apache.zookeeper.proto.CreateRequest)15 IOException (java.io.IOException)9 ByteBuffer (java.nio.ByteBuffer)9 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)9 SetDataRequest (org.apache.zookeeper.proto.SetDataRequest)9 TxnHeader (org.apache.zookeeper.txn.TxnHeader)9 HashSet (java.util.HashSet)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)6 Record (org.apache.jute.Record)6 KeeperException (org.apache.zookeeper.KeeperException)5 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)5 Id (org.apache.zookeeper.data.Id)4 ErrorTxn (org.apache.zookeeper.txn.ErrorTxn)4 OutputArchive (org.apache.jute.OutputArchive)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 TxnLogEntry (org.apache.zookeeper.server.TxnLogEntry)3 QuorumVerifier (org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)3