@Override public SegmentMetadataClient createSegmentMetadataClient(Segment segment, String delegationToken) { return new SegmentMetadataClientImpl(segment, controller, cf, delegationToken); }
private CompletableFuture<SegmentAttributeUpdated> updatePropertyAsync(UUID attributeId, long expected, long value, String delegationToken) { long requestId = requestIdGenerator.get(); log.trace("Updating segment attribute: {}", attributeId); RawClient connection = getConnection(); return connection.sendRequest(requestId, new UpdateSegmentAttribute(requestId, segmentId.getScopedName(), attributeId, value, expected, delegationToken)) .thenApply(r -> transformReply(r, SegmentAttributeUpdated.class)); }
@Override public void close() { log.info("Closing segment metadata connection for {}", segmentId); if (closed.compareAndSet(false, true)) { closeConnection(new ConnectionClosedException()); } }
cf.provideConnection(endpoint, connection); @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); client.getConnection(); WireCommands.GetStreamSegmentInfo getSegmentInfo1 = new WireCommands.GetStreamSegmentInfo(1, segment.getScopedName(), ""); Mockito.doAnswer(new Answer<Void>() { long length = client.fetchCurrentSegmentLength(); InOrder order = Mockito.inOrder(connection, cf); order.verify(cf).establishConnection(eq(endpoint), any(ReplyProcessor.class));
@Test(timeout = 10000) public void compareAndSetAttribute() throws Exception { UUID attributeId = SegmentAttribute.RevisionStreamClientMark.getValue(); Segment segment = new Segment("scope", "testRetry", 4); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(endpoint, connection); @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); client.getConnection(); ReplyProcessor processor = cf.getProcessor(endpoint); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { processor.process(new SegmentAttributeUpdated(1, true)); return null; } }).when(connection).sendAsync(Mockito.eq(new WireCommands.UpdateSegmentAttribute(1, segment.getScopedName(), attributeId, 1234, -1234, "")), Mockito.any(ClientConnection.CompletedCallback.class)); assertTrue(client.compareAndSetAttribute(SegmentAttribute.RevisionStreamClientMark, -1234, 1234)); }
@Test(timeout = 10000) public void testTruncate() throws Exception { Segment segment = new Segment("scope", "testTruncate", 4); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0); @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf); @Cleanup ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(endpoint, connection); @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); client.getConnection(); ReplyProcessor processor = cf.getProcessor(endpoint); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { processor.process(new SegmentTruncated(1, segment.getScopedName())); return null; } }).when(connection).sendAsync(Mockito.eq(new WireCommands.TruncateSegment(1, segment.getScopedName(), 123L, "")), Mockito.any(ClientConnection.CompletedCallback.class)); client.truncateSegment(123L); Mockito.verify(connection).sendAsync(Mockito.eq(new WireCommands.TruncateSegment(1, segment.getScopedName(), 123L, "")), Mockito.any(ClientConnection.CompletedCallback.class)); }
@Test(timeout = 10000) public void testSeal() throws Exception { Segment segment = new Segment("scope", "testSeal", 4); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0); @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf); @Cleanup ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(endpoint, connection); @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); client.getConnection(); ReplyProcessor processor = cf.getProcessor(endpoint); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { processor.process(new WireCommands.SegmentSealed(1, segment.getScopedName())); return null; } }).when(connection).sendAsync(Mockito.eq(new WireCommands.SealSegment(1, segment.getScopedName(), "")), Mockito.any(ClientConnection.CompletedCallback.class)); client.sealSegment(); Mockito.verify(connection).sendAsync(Mockito.eq(new WireCommands.SealSegment(1, segment.getScopedName(), "")), Mockito.any(ClientConnection.CompletedCallback.class)); }
@Test(timeout = 10000) public void testGetProperty() throws Exception { UUID attributeId = SegmentAttribute.RevisionStreamClientMark.getValue(); Segment segment = new Segment("scope", "testRetry", 4); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0); @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf); @Cleanup ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(endpoint, connection); @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); client.getConnection(); ReplyProcessor processor = cf.getProcessor(endpoint); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { processor.process(new WireCommands.SegmentAttribute(1, 123)); return null; } }).when(connection).sendAsync(Mockito.eq(new WireCommands.GetSegmentAttribute(1, segment.getScopedName(), attributeId, "")), Mockito.any(ClientConnection.CompletedCallback.class)); long value = client.fetchProperty(SegmentAttribute.RevisionStreamClientMark); assertEquals(123, value); }
SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); InOrder order = Mockito.inOrder(connection1, connection2, cf); long length = client.fetchCurrentSegmentLength(); order.verify(cf, Mockito.times(2)).establishConnection(Mockito.eq(endpoint), Mockito.any()); order.verify(connection1).sendAsync(Mockito.eq(getSegmentInfo1), Mockito.any(ClientConnection.CompletedCallback.class));
@Override public long fetchCurrentSegmentLength() { Exceptions.checkNotClosed(closed.get(), this); val future = RETRY_SCHEDULE.retryingOn(ConnectionFailedException.class) .throwingOn(NoSuchSegmentException.class) .runAsync(() -> getStreamSegmentInfo(delegationToken), connectionFactory.getInternalExecutor()); return Futures.getThrowingException(future).getWriteOffset(); }
@Override public void sealSegment() { val future = RETRY_SCHEDULE.retryingOn(ConnectionFailedException.class) .throwingOn(NoSuchSegmentException.class) .runAsync(() -> sealSegmentAsync(segmentId, delegationToken), connectionFactory.getInternalExecutor()); future.join(); }
@Override public long fetchProperty(SegmentAttribute attribute) { Exceptions.checkNotClosed(closed.get(), this); val future = RETRY_SCHEDULE.retryingOn(ConnectionFailedException.class) .throwingOn(NoSuchSegmentException.class) .runAsync(() -> getPropertyAsync(attribute.getValue(), delegationToken), connectionFactory.getInternalExecutor()); return Futures.getThrowingException(future).getValue(); }
@Test(timeout = 10000) public void testCurrentStreamLength() throws Exception { Segment segment = new Segment("scope", "testRetry", 4); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0); @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf); @Cleanup ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(endpoint, connection); @Cleanup SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, ""); client.getConnection(); ReplyProcessor processor = cf.getProcessor(endpoint); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { processor.process(new StreamSegmentInfo(1, segment.getScopedName(), true, false, false, 0, 123, 121)); return null; } }).when(connection).sendAsync(Mockito.eq(new WireCommands.GetStreamSegmentInfo(1, segment.getScopedName(), "")), Mockito.any(ClientConnection.CompletedCallback.class)); long length = client.fetchCurrentSegmentLength(); assertEquals(123, length); }
@Override public SegmentInfo getSegmentInfo() { val future = RETRY_SCHEDULE.retryingOn(ConnectionFailedException.class) .throwingOn(NoSuchSegmentException.class) .runAsync(() -> getStreamSegmentInfo(delegationToken), connectionFactory.getInternalExecutor()); StreamSegmentInfo info = Futures.getThrowingException(future); return new SegmentInfo(segmentId, info.getStartOffset(), info.getWriteOffset(), info.isSealed(), info.getLastModified()); }
private CompletableFuture<StreamSegmentInfo> getStreamSegmentInfo(String delegationToken) { long requestId = requestIdGenerator.get(); log.debug("Getting segment info for segment: {}", segmentId); RawClient connection = getConnection(); return connection.sendRequest(requestId, new GetStreamSegmentInfo(requestId, segmentId.getScopedName(), delegationToken)) .thenApply(r -> transformReply(r, StreamSegmentInfo.class)); }
@SuppressWarnings("unchecked") @SneakyThrows(ConnectionFailedException.class) private <T extends Reply> T transformReply(Reply reply, Class<T> klass) { if (klass.isAssignableFrom(reply.getClass())) { return (T) reply; } closeConnection(reply); if (reply instanceof WireCommands.NoSuchSegment) { throw new NoSuchSegmentException(reply.toString()); } else if (reply instanceof WrongHost) { throw new ConnectionFailedException(reply.toString()); } else { throw new ConnectionFailedException("Unexpected reply of " + reply + " when expecting a " + klass.getName()); } }
private CompletableFuture<SegmentTruncated> truncateSegmentAsync(Segment segment, long offset, String delegationToken) { long requestId = requestIdGenerator.get(); log.trace("Truncating segment: {}", segment); RawClient connection = getConnection(); return connection.sendRequest(requestId, new TruncateSegment(requestId, segment.getScopedName(), offset, delegationToken)) .thenApply(r -> transformReply(r, SegmentTruncated.class)); }
private CompletableFuture<WireCommands.SegmentAttribute> getPropertyAsync(UUID attributeId, String delegationToken) { long requestId = requestIdGenerator.get(); log.debug("Getting segment attribute: {}", attributeId); RawClient connection = getConnection(); return connection.sendRequest(requestId, new GetSegmentAttribute(requestId, segmentId.getScopedName(), attributeId, delegationToken)) .thenApply(r -> transformReply(r, WireCommands.SegmentAttribute.class)); }
private CompletableFuture<SegmentSealed> sealSegmentAsync(Segment segment, String delegationToken) { long requestId = requestIdGenerator.get(); log.trace("Sealing segment: {}", segment); RawClient connection = getConnection(); return connection.sendRequest(requestId, new SealSegment(requestId, segment.getScopedName(), delegationToken)) .thenApply(r -> transformReply(r, SegmentSealed.class)); }