use of io.pravega.client.admin.KeyValueTableInfo in project pravega by pravega.
the class StreamManagerImpl method deleteScope.
/**
* A new API is created hence this is going to be deprecated.
*
* @deprecated As of Pravega release 0.11.0, replaced by {@link #deleteScopeRecursive(String)}.
*/
@Override
@Deprecated
public boolean deleteScope(String scopeName, boolean forceDelete) throws DeleteScopeFailedException {
NameUtils.validateUserScopeName(scopeName);
if (forceDelete) {
log.info("Deleting scope recursively: {}", scopeName);
List<String> readerGroupList = new ArrayList<>();
Iterator<Stream> iterator = listStreams(scopeName);
while (iterator.hasNext()) {
Stream stream = iterator.next();
if (stream.getStreamName().startsWith(READER_GROUP_STREAM_PREFIX)) {
readerGroupList.add(stream.getStreamName().substring(READER_GROUP_STREAM_PREFIX.length()));
}
try {
Futures.getThrowingException(Futures.exceptionallyExpecting(controller.sealStream(stream.getScope(), stream.getStreamName()), e -> {
Throwable unwrap = Exceptions.unwrap(e);
// ignore failures if the stream doesn't exist or we are unable to seal it.
return unwrap instanceof InvalidStreamException || unwrap instanceof ControllerFailureException;
}, false).thenCompose(sealed -> controller.deleteStream(stream.getScope(), stream.getStreamName())));
} catch (Exception e) {
String message = String.format("Failed to seal and delete stream %s", stream.getStreamName());
throw new DeleteScopeFailedException(message, e);
}
}
Iterator<KeyValueTableInfo> kvtIterator = controller.listKeyValueTables(scopeName).asIterator();
while (kvtIterator.hasNext()) {
KeyValueTableInfo kvt = kvtIterator.next();
try {
Futures.getThrowingException(controller.deleteKeyValueTable(scopeName, kvt.getKeyValueTableName()));
} catch (Exception e) {
String message = String.format("Failed to delete key-value table %s", kvt.getKeyValueTableName());
throw new DeleteScopeFailedException(message, e);
}
}
for (String groupName : readerGroupList) {
try {
Futures.getThrowingException(controller.getReaderGroupConfig(scopeName, groupName).thenCompose(conf -> controller.deleteReaderGroup(scopeName, groupName, conf.getReaderGroupId())));
} catch (Exception e) {
if (Exceptions.unwrap(e) instanceof ReaderGroupNotFoundException) {
continue;
}
String message = String.format("Failed to delete reader group %s", groupName);
throw new DeleteScopeFailedException(message, e);
}
}
}
return Futures.getThrowingException(controller.deleteScope(scopeName));
}
use of io.pravega.client.admin.KeyValueTableInfo in project pravega by pravega.
the class StreamManagerImplTest method testForceDeleteScopeWithKeyValueTables.
@Test(timeout = 10000)
public void testForceDeleteScopeWithKeyValueTables() throws ConnectionFailedException, DeleteScopeFailedException {
// Setup Mocks
ClientConnection connection = mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.CreateTableSegment request = (WireCommands.CreateTableSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.CreateTableSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.CreateTableSegment request = invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.CreateTableSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.DeleteSegment request = (WireCommands.DeleteSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentDeleted(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.DeleteSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.DeleteTableSegment request = invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentDeleted(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.DeleteTableSegment.class));
connectionFactory.provideConnection(location, connection);
MockController mockController = spy(new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true));
ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
@Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
@Cleanup final KeyValueTableManager keyValueTableManager = new KeyValueTableManagerImpl(mockController, connectionFactory);
String scope = "scope";
String kvt1 = "kvt1";
String kvt2 = "kvt2";
streamManager.createScope(scope);
KeyValueTableConfiguration kvtConfig = KeyValueTableConfiguration.builder().partitionCount(1).primaryKeyLength(1).secondaryKeyLength(1).build();
keyValueTableManager.createKeyValueTable(scope, kvt1, kvtConfig);
keyValueTableManager.createKeyValueTable(scope, kvt2, kvtConfig);
Set<KeyValueTableInfo> keyValueTables = Sets.newHashSet(keyValueTableManager.listKeyValueTables(scope));
assertEquals(2, keyValueTables.size());
assertTrue(keyValueTables.stream().anyMatch(x -> x.getKeyValueTableName().equals(kvt1)));
assertTrue(keyValueTables.stream().anyMatch(x -> x.getKeyValueTableName().equals(kvt2)));
// mock controller client to throw exceptions when attempting to delete key value table 1.
doAnswer(x -> Futures.failedFuture(new ControllerFailureException("Unable to delete key value table"))).when(mockController).deleteKeyValueTable(scope, kvt1);
AssertExtensions.assertThrows("Should have thrown exception", () -> streamManager.deleteScope(scope, true), e -> Exceptions.unwrap(e) instanceof DeleteScopeFailedException);
// reset mock controller
reset(mockController);
assertTrue(streamManager.deleteScope(scope, true));
}
use of io.pravega.client.admin.KeyValueTableInfo in project pravega by pravega.
the class KeyValueTableManagerImplTest method testListKeyValueTables.
/**
* Tests the following method(s):
* - {@link KeyValueTableManagerImpl#listKeyValueTables}
*/
@Test
public void testListKeyValueTables() {
@Cleanup val manager = new KeyValueTableManagerImpl(this.controller, this.connectionFactory);
final String[] names = new String[] { "kvt1", "kvt2", "kvt3", "kvt4" };
Assert.assertFalse(manager.listKeyValueTables(DEFAULT_SCOPE).hasNext());
for (String name : names) {
manager.createKeyValueTable(DEFAULT_SCOPE, name, DEFAULT_CONFIG);
// Do it twice.
manager.createKeyValueTable(DEFAULT_SCOPE, name, DEFAULT_CONFIG);
}
val listResult = Streams.stream(manager.listKeyValueTables(DEFAULT_SCOPE)).map(KeyValueTableInfo::getScopedName).sorted().toArray(String[]::new);
val expectedListResult = Arrays.stream(names).map(name -> new KeyValueTableInfo(DEFAULT_SCOPE, name).getScopedName()).sorted().toArray(String[]::new);
Assert.assertArrayEquals(expectedListResult, listResult);
}
use of io.pravega.client.admin.KeyValueTableInfo in project pravega by pravega.
the class KeyValueTableTestBase method testNoSecondaryKeys.
/**
* Tests functionality without secondary keys. Only single-key operations are tested because we cannot do multi-key
* operations using different primary keys.
* @throws Exception If an error occurred.
*/
@Test
public void testNoSecondaryKeys() throws Exception {
val entryCount = getPrimaryKeyCount();
val config = KeyValueTableConfiguration.builder().partitionCount(DEFAULT_SEGMENT_COUNT).primaryKeyLength(16).secondaryKeyLength(// No Secondary Keys.
0).build();
val rnd = new Random(0);
@Cleanup val kvt = createKeyValueTable(new KeyValueTableInfo("Scope", "KVTNoSecondaryKeys"), config);
List<ByteBuffer> keys = IntStream.range(0, entryCount).mapToObj(i -> {
val keyData = new byte[config.getPrimaryKeyLength()];
rnd.nextBytes(keyData);
return ByteBuffer.wrap(keyData).asReadOnlyBuffer();
}).collect(Collectors.toList());
// Insertions.
val iteration = new AtomicInteger(0);
val versions = new HashMap<ByteBuffer, Version>();
for (ByteBuffer k : keys) {
Version v = kvt.update(new Insert(new TableKey(k), getValue(k, iteration.get()))).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
versions.put(k, v);
AssertExtensions.assertSuppliedFutureThrows("Conditional insert did not fail for existing key.", () -> kvt.update(new Insert(new TableKey(k), getValue(k, iteration.get()))), ex -> ex instanceof BadKeyVersionException);
}
checkValues(iteration.get(), versions, kvt);
// Updates.
iteration.incrementAndGet();
for (ByteBuffer k : keys) {
Version v = versions.get(k);
long pkValue = Math.abs(PK_SERIALIZER.deserialize(k));
Put p = pkValue % 2 == 0 ? new Put(new TableKey(k), getValue(k, iteration.get()), v) : new Put(new TableKey(k), getValue(k, iteration.get()));
v = kvt.update(p).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
versions.put(k, v);
}
checkValues(iteration.get(), versions, kvt);
// Removals.
iteration.incrementAndGet();
for (ByteBuffer k : keys) {
Version v = versions.get(k);
long pkValue = Math.abs(PK_SERIALIZER.deserialize(k));
Remove r = pkValue % 2 == 0 ? new Remove(new TableKey(k), v) : new Remove(new TableKey(k));
kvt.update(r).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
versions.put(k, null);
}
checkValues(iteration.get(), versions, kvt);
}
use of io.pravega.client.admin.KeyValueTableInfo in project pravega by pravega.
the class KeyValueTableTest method testCreateListKeyValueTable.
/**
* Smoke Test. Verify that the KeyValueTable can be created and listed.
*/
@Test
public void testCreateListKeyValueTable() {
val kvt1 = newKeyValueTableName();
boolean created = this.controller.createKeyValueTable(kvt1.getScope(), kvt1.getKeyValueTableName(), DEFAULT_CONFIG).join();
Assert.assertTrue(created);
val segments = this.controller.getCurrentSegmentsForKeyValueTable(kvt1.getScope(), kvt1.getKeyValueTableName()).join();
Assert.assertEquals(DEFAULT_CONFIG.getPartitionCount(), segments.getSegments().size());
for (val s : segments.getSegments()) {
// We know there's nothing in these segments. But if the segments hadn't been created, then this will throw
// an exception.
this.tableStore.get(s.getKVTScopedName(), Collections.singletonList(new ByteArraySegment(new byte[DEFAULT_CONFIG.getTotalKeyLength()])), TIMEOUT).join();
}
// Verify re-creation does not work.
Assert.assertFalse(this.controller.createKeyValueTable(kvt1.getScope(), kvt1.getKeyValueTableName(), DEFAULT_CONFIG).join());
// Try to create a KVTable with 0 partitions, and it should fail
val kvtZero = newKeyValueTableName();
assertThrows(IllegalArgumentException.class, () -> this.controller.createKeyValueTable(kvtZero.getScope(), kvtZero.getKeyValueTableName(), KeyValueTableConfiguration.builder().partitionCount(0).build()).join());
// Create 2 more KVTables
val kvt2 = newKeyValueTableName();
created = this.controller.createKeyValueTable(kvt2.getScope(), kvt2.getKeyValueTableName(), DEFAULT_CONFIG).join();
Assert.assertTrue(created);
val kvt3 = newKeyValueTableName();
created = this.controller.createKeyValueTable(kvt3.getScope(), kvt3.getKeyValueTableName(), DEFAULT_CONFIG).join();
Assert.assertTrue(created);
// Check list tables...
AsyncIterator<KeyValueTableInfo> kvTablesIterator = this.controller.listKeyValueTables(SCOPE);
Iterator<KeyValueTableInfo> iter = kvTablesIterator.asIterator();
Map<String, Integer> countMap = new HashMap<String, Integer>(3);
while (iter.hasNext()) {
KeyValueTableInfo kvtInfo = iter.next();
if (kvtInfo.getScope().equals(SCOPE)) {
if (countMap.containsKey(kvtInfo.getKeyValueTableName())) {
Integer newCount = Integer.valueOf(countMap.get(kvtInfo.getKeyValueTableName()).intValue() + 1);
countMap.put(iter.next().getKeyValueTableName(), newCount);
} else {
countMap.put(kvtInfo.getKeyValueTableName(), 1);
}
}
}
Assert.assertEquals(3, countMap.size());
Assert.assertEquals(1, countMap.get(kvt1.getKeyValueTableName()).intValue());
Assert.assertEquals(1, countMap.get(kvt2.getKeyValueTableName()).intValue());
Assert.assertEquals(1, countMap.get(kvt3.getKeyValueTableName()).intValue());
}
Aggregations