SegmentAttribute() { HashHelper hash = HashHelper.seededWith("SegmentAttribute"); value = hash.toUUID(this.name()); } }
@Override public boolean compareAndSetAttribute(SegmentAttribute attribute, long expectedValue, long newValue) { Exceptions.checkNotClosed(closed.get(), this); val future = RETRY_SCHEDULE.retryingOn(ConnectionFailedException.class) .throwingOn(NoSuchSegmentException.class) .runAsync(() -> updatePropertyAsync(attribute.getValue(), expectedValue, newValue, delegationToken), connectionFactory.getInternalExecutor()); return Futures.getThrowingException(future).isSuccess(); }
@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 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 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); }