use of org.opensearch.common.Table in project OpenSearch by opensearch-project.
the class RestRecoveryActionTests method testRestRecoveryAction.
public void testRestRecoveryAction() {
final RestCatRecoveryAction action = new RestCatRecoveryAction();
final int totalShards = randomIntBetween(1, 32);
final int successfulShards = Math.max(0, totalShards - randomIntBetween(1, 2));
final int failedShards = totalShards - successfulShards;
final Map<String, List<RecoveryState>> shardRecoveryStates = new HashMap<>();
final List<RecoveryState> recoveryStates = new ArrayList<>();
for (int i = 0; i < successfulShards; i++) {
final RecoveryState state = mock(RecoveryState.class);
when(state.getShardId()).thenReturn(new ShardId(new Index("index", "_na_"), i));
final RecoveryState.Timer timer = mock(RecoveryState.Timer.class);
final long startTime = randomLongBetween(0, new Date().getTime());
when(timer.startTime()).thenReturn(startTime);
final long time = randomLongBetween(1000000, 10 * 1000000);
when(timer.time()).thenReturn(time);
when(timer.stopTime()).thenReturn(startTime + time);
when(state.getTimer()).thenReturn(timer);
when(state.getRecoverySource()).thenReturn(TestShardRouting.randomRecoverySource());
when(state.getStage()).thenReturn(randomFrom(RecoveryState.Stage.values()));
final DiscoveryNode sourceNode = randomBoolean() ? mock(DiscoveryNode.class) : null;
if (sourceNode != null) {
when(sourceNode.getHostName()).thenReturn(randomAlphaOfLength(8));
}
when(state.getSourceNode()).thenReturn(sourceNode);
final DiscoveryNode targetNode = mock(DiscoveryNode.class);
when(targetNode.getHostName()).thenReturn(randomAlphaOfLength(8));
when(state.getTargetNode()).thenReturn(targetNode);
RecoveryState.Index index = mock(RecoveryState.Index.class);
final int totalRecoveredFiles = randomIntBetween(1, 64);
when(index.totalRecoverFiles()).thenReturn(totalRecoveredFiles);
final int recoveredFileCount = randomIntBetween(0, totalRecoveredFiles);
when(index.recoveredFileCount()).thenReturn(recoveredFileCount);
when(index.recoveredFilesPercent()).thenReturn((100f * recoveredFileCount) / totalRecoveredFiles);
when(index.totalFileCount()).thenReturn(randomIntBetween(totalRecoveredFiles, 2 * totalRecoveredFiles));
final int totalRecoveredBytes = randomIntBetween(1, 1 << 24);
when(index.totalRecoverBytes()).thenReturn((long) totalRecoveredBytes);
final int recoveredBytes = randomIntBetween(0, totalRecoveredBytes);
when(index.recoveredBytes()).thenReturn((long) recoveredBytes);
when(index.recoveredBytesPercent()).thenReturn((100f * recoveredBytes) / totalRecoveredBytes);
when(index.totalRecoverBytes()).thenReturn((long) randomIntBetween(totalRecoveredBytes, 2 * totalRecoveredBytes));
when(state.getIndex()).thenReturn(index);
final RecoveryState.Translog translog = mock(RecoveryState.Translog.class);
final int translogOps = randomIntBetween(0, 1 << 18);
when(translog.totalOperations()).thenReturn(translogOps);
final int translogOpsRecovered = randomIntBetween(0, translogOps);
when(translog.recoveredOperations()).thenReturn(translogOpsRecovered);
when(translog.recoveredPercent()).thenReturn(translogOps == 0 ? 100f : (100f * translogOpsRecovered / translogOps));
when(state.getTranslog()).thenReturn(translog);
recoveryStates.add(state);
}
final List<RecoveryState> shuffle = new ArrayList<>(recoveryStates);
Randomness.shuffle(shuffle);
shardRecoveryStates.put("index", shuffle);
final List<DefaultShardOperationFailedException> shardFailures = new ArrayList<>();
final RecoveryResponse response = new RecoveryResponse(totalShards, successfulShards, failedShards, shardRecoveryStates, shardFailures);
final Table table = action.buildRecoveryTable(null, response);
assertNotNull(table);
List<Table.Cell> headers = table.getHeaders();
final List<String> expectedHeaders = Arrays.asList("index", "shard", "start_time", "start_time_millis", "stop_time", "stop_time_millis", "time", "type", "stage", "source_host", "source_node", "target_host", "target_node", "repository", "snapshot", "files", "files_recovered", "files_percent", "files_total", "bytes", "bytes_recovered", "bytes_percent", "bytes_total", "translog_ops", "translog_ops_recovered", "translog_ops_percent");
for (int i = 0; i < expectedHeaders.size(); i++) {
assertThat(headers.get(i).value, equalTo(expectedHeaders.get(i)));
}
assertThat(table.getRows().size(), equalTo(successfulShards));
for (int i = 0; i < successfulShards; i++) {
final RecoveryState state = recoveryStates.get(i);
final List<Object> expectedValues = Arrays.asList("index", i, XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().startTime()), state.getTimer().startTime(), XContentOpenSearchExtension.DEFAULT_DATE_PRINTER.print(state.getTimer().stopTime()), state.getTimer().stopTime(), new TimeValue(state.getTimer().time()), state.getRecoverySource().getType().name().toLowerCase(Locale.ROOT), state.getStage().name().toLowerCase(Locale.ROOT), state.getSourceNode() == null ? "n/a" : state.getSourceNode().getHostName(), state.getSourceNode() == null ? "n/a" : state.getSourceNode().getName(), state.getTargetNode().getHostName(), state.getTargetNode().getName(), state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getRepository(), state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getSnapshotId().getName(), state.getIndex().totalRecoverFiles(), state.getIndex().recoveredFileCount(), percent(state.getIndex().recoveredFilesPercent()), state.getIndex().totalFileCount(), state.getIndex().totalRecoverBytes(), state.getIndex().recoveredBytes(), percent(state.getIndex().recoveredBytesPercent()), state.getIndex().totalBytes(), state.getTranslog().totalOperations(), state.getTranslog().recoveredOperations(), percent(state.getTranslog().recoveredPercent()));
final List<Table.Cell> cells = table.getRows().get(i);
for (int j = 0; j < expectedValues.size(); j++) {
assertThat(cells.get(j).value, equalTo(expectedValues.get(j)));
}
}
}
use of org.opensearch.common.Table in project OpenSearch by opensearch-project.
the class RestTableTests method setup.
@Before
public void setup() {
restRequest = new FakeRestRequest();
table = new Table();
table.startHeaders();
table.addCell("bulk.foo", "alias:f;desc:foo");
table.addCell("bulk.bar", "alias:b;desc:bar");
// should be matched as well due to the aliases
table.addCell("aliasedBulk", "alias:bulkWhatever;desc:bar");
table.addCell("aliasedSecondBulk", "alias:foobar,bulkolicious,bulkotastic;desc:bar");
// no match
table.addCell("unmatched", "alias:un.matched;desc:bar");
// invalid alias
table.addCell("invalidAliasesBulk", "alias:,,,;desc:bar");
// timestamp
table.addCell("timestamp", "alias:ts");
table.addCell("epoch", "alias:t");
table.endHeaders();
}
use of org.opensearch.common.Table in project OpenSearch by opensearch-project.
the class RestTableTests method testMultiSort.
public void testMultiSort() {
Table table = new Table();
table.startHeaders();
table.addCell("compare");
table.addCell("second.compare");
table.endHeaders();
List<Integer> comparisonList = Arrays.asList(3, 3, 2);
List<Integer> secondComparisonList = Arrays.asList(2, 1, 3);
for (int i = 0; i < comparisonList.size(); i++) {
table.startRow();
table.addCell(comparisonList.get(i));
table.addCell(secondComparisonList.get(i));
table.endRow();
}
restRequest.params().put("s", "compare,second.compare");
List<Integer> rowOrder = RestTable.getRowOrder(table, restRequest);
assertEquals(Arrays.asList(2, 1, 0), rowOrder);
restRequest.params().put("s", "compare:desc,second.compare");
rowOrder = RestTable.getRowOrder(table, restRequest);
assertEquals(Arrays.asList(1, 0, 2), rowOrder);
}
use of org.opensearch.common.Table in project OpenSearch by opensearch-project.
the class RestTableTests method testRowOutOfBounds.
public void testRowOutOfBounds() {
Table table = new Table();
table.startHeaders();
table.addCell("compare");
table.endHeaders();
RestTable.TableIndexComparator comparator = new RestTable.TableIndexComparator(table, Collections.singletonList(new RestTable.ColumnOrderElement("compare", false)));
Error e = expectThrows(AssertionError.class, () -> {
comparator.compare(0, 1);
});
assertEquals("Invalid comparison of indices (0, 1): Table has 0 rows.", e.getMessage());
}
use of org.opensearch.common.Table in project OpenSearch by opensearch-project.
the class RestTableTests method testAliasSort.
public void testAliasSort() {
Table table = new Table();
table.startHeaders();
table.addCell("compare", "alias:c;");
table.endHeaders();
List<Integer> comparisonList = Arrays.asList(3, 1, 2);
for (int i = 0; i < comparisonList.size(); i++) {
table.startRow();
table.addCell(comparisonList.get(i));
table.endRow();
}
restRequest.params().put("s", "c");
List<Integer> rowOrder = RestTable.getRowOrder(table, restRequest);
assertEquals(Arrays.asList(1, 2, 0), rowOrder);
}
Aggregations