use of org.elasticsearch.rest.RestController in project elasticsearch by elastic.
the class RestRecoveryActionTests method testRestRecoveryAction.
public void testRestRecoveryAction() {
final Settings settings = Settings.EMPTY;
final RestController restController = new RestController(settings, Collections.emptySet(), null, null, null);
final RestRecoveryAction action = new RestRecoveryAction(settings, restController);
final int totalShards = randomIntBetween(1, 32);
final int successfulShards = Math.max(0, totalShards - randomIntBetween(1, 2));
final int failedShards = totalShards - successfulShards;
final boolean detailed = randomBoolean();
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);
when(timer.time()).thenReturn((long) randomIntBetween(1000000, 10 * 1000000));
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(randomAsciiOfLength(8));
}
when(state.getSourceNode()).thenReturn(sourceNode);
final DiscoveryNode targetNode = mock(DiscoveryNode.class);
when(targetNode.getHostName()).thenReturn(randomAsciiOfLength(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<ShardOperationFailedException> shardFailures = new ArrayList<>();
final RecoveryResponse response = new RecoveryResponse(totalShards, successfulShards, failedShards, detailed, shardRecoveryStates, shardFailures);
final Table table = action.buildRecoveryTable(null, response);
assertNotNull(table);
List<Table.Cell> headers = table.getHeaders();
assertThat(headers.get(0).value, equalTo("index"));
assertThat(headers.get(1).value, equalTo("shard"));
assertThat(headers.get(2).value, equalTo("time"));
assertThat(headers.get(3).value, equalTo("type"));
assertThat(headers.get(4).value, equalTo("stage"));
assertThat(headers.get(5).value, equalTo("source_host"));
assertThat(headers.get(6).value, equalTo("source_node"));
assertThat(headers.get(7).value, equalTo("target_host"));
assertThat(headers.get(8).value, equalTo("target_node"));
assertThat(headers.get(9).value, equalTo("repository"));
assertThat(headers.get(10).value, equalTo("snapshot"));
assertThat(headers.get(11).value, equalTo("files"));
assertThat(headers.get(12).value, equalTo("files_recovered"));
assertThat(headers.get(13).value, equalTo("files_percent"));
assertThat(headers.get(14).value, equalTo("files_total"));
assertThat(headers.get(15).value, equalTo("bytes"));
assertThat(headers.get(16).value, equalTo("bytes_recovered"));
assertThat(headers.get(17).value, equalTo("bytes_percent"));
assertThat(headers.get(18).value, equalTo("bytes_total"));
assertThat(headers.get(19).value, equalTo("translog_ops"));
assertThat(headers.get(20).value, equalTo("translog_ops_recovered"));
assertThat(headers.get(21).value, equalTo("translog_ops_percent"));
assertThat(table.getRows().size(), equalTo(successfulShards));
for (int i = 0; i < successfulShards; i++) {
final RecoveryState state = recoveryStates.get(i);
List<Table.Cell> cells = table.getRows().get(i);
assertThat(cells.get(0).value, equalTo("index"));
assertThat(cells.get(1).value, equalTo(i));
assertThat(cells.get(2).value, equalTo(new TimeValue(state.getTimer().time())));
assertThat(cells.get(3).value, equalTo(state.getRecoverySource().getType().name().toLowerCase(Locale.ROOT)));
assertThat(cells.get(4).value, equalTo(state.getStage().name().toLowerCase(Locale.ROOT)));
assertThat(cells.get(5).value, equalTo(state.getSourceNode() == null ? "n/a" : state.getSourceNode().getHostName()));
assertThat(cells.get(6).value, equalTo(state.getSourceNode() == null ? "n/a" : state.getSourceNode().getName()));
assertThat(cells.get(7).value, equalTo(state.getTargetNode().getHostName()));
assertThat(cells.get(8).value, equalTo(state.getTargetNode().getName()));
assertThat(cells.get(9).value, equalTo(state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getRepository()));
assertThat(cells.get(10).value, equalTo(state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getSnapshotId().getName()));
assertThat(cells.get(11).value, equalTo(state.getIndex().totalRecoverFiles()));
assertThat(cells.get(12).value, equalTo(state.getIndex().recoveredFileCount()));
assertThat(cells.get(13).value, equalTo(percent(state.getIndex().recoveredFilesPercent())));
assertThat(cells.get(14).value, equalTo(state.getIndex().totalFileCount()));
assertThat(cells.get(15).value, equalTo(state.getIndex().totalRecoverBytes()));
assertThat(cells.get(16).value, equalTo(state.getIndex().recoveredBytes()));
assertThat(cells.get(17).value, equalTo(percent(state.getIndex().recoveredBytesPercent())));
assertThat(cells.get(18).value, equalTo(state.getIndex().totalBytes()));
assertThat(cells.get(19).value, equalTo(state.getTranslog().totalOperations()));
assertThat(cells.get(20).value, equalTo(state.getTranslog().recoveredOperations()));
assertThat(cells.get(21).value, equalTo(percent(state.getTranslog().recoveredPercent())));
}
}
use of org.elasticsearch.rest.RestController in project elasticsearch by elastic.
the class ActionModule method initRestHandlers.
public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
List<AbstractCatAction> catActions = new ArrayList<>();
Consumer<RestHandler> registerHandler = a -> {
if (a instanceof AbstractCatAction) {
catActions.add((AbstractCatAction) a);
}
};
registerHandler.accept(new RestMainAction(settings, restController));
registerHandler.accept(new RestNodesInfoAction(settings, restController, settingsFilter));
registerHandler.accept(new RestNodesStatsAction(settings, restController));
registerHandler.accept(new RestNodesHotThreadsAction(settings, restController));
registerHandler.accept(new RestClusterAllocationExplainAction(settings, restController));
registerHandler.accept(new RestClusterStatsAction(settings, restController));
registerHandler.accept(new RestClusterStateAction(settings, restController, settingsFilter));
registerHandler.accept(new RestClusterHealthAction(settings, restController));
registerHandler.accept(new RestClusterUpdateSettingsAction(settings, restController));
registerHandler.accept(new RestClusterGetSettingsAction(settings, restController, clusterSettings, settingsFilter));
registerHandler.accept(new RestClusterRerouteAction(settings, restController, settingsFilter));
registerHandler.accept(new RestClusterSearchShardsAction(settings, restController));
registerHandler.accept(new RestPendingClusterTasksAction(settings, restController));
registerHandler.accept(new RestPutRepositoryAction(settings, restController));
registerHandler.accept(new RestGetRepositoriesAction(settings, restController, settingsFilter));
registerHandler.accept(new RestDeleteRepositoryAction(settings, restController));
registerHandler.accept(new RestVerifyRepositoryAction(settings, restController));
registerHandler.accept(new RestGetSnapshotsAction(settings, restController));
registerHandler.accept(new RestCreateSnapshotAction(settings, restController));
registerHandler.accept(new RestRestoreSnapshotAction(settings, restController));
registerHandler.accept(new RestDeleteSnapshotAction(settings, restController));
registerHandler.accept(new RestSnapshotsStatusAction(settings, restController));
registerHandler.accept(new RestTypesExistsAction(settings, restController));
registerHandler.accept(new RestGetIndicesAction(settings, restController, indexScopedSettings, settingsFilter));
registerHandler.accept(new RestIndicesStatsAction(settings, restController));
registerHandler.accept(new RestIndicesSegmentsAction(settings, restController));
registerHandler.accept(new RestIndicesShardStoresAction(settings, restController));
registerHandler.accept(new RestGetAliasesAction(settings, restController));
registerHandler.accept(new RestIndexDeleteAliasesAction(settings, restController));
registerHandler.accept(new RestIndexPutAliasAction(settings, restController));
registerHandler.accept(new RestIndicesAliasesAction(settings, restController));
registerHandler.accept(new RestCreateIndexAction(settings, restController));
registerHandler.accept(new RestShrinkIndexAction(settings, restController));
registerHandler.accept(new RestRolloverIndexAction(settings, restController));
registerHandler.accept(new RestDeleteIndexAction(settings, restController));
registerHandler.accept(new RestCloseIndexAction(settings, restController));
registerHandler.accept(new RestOpenIndexAction(settings, restController));
registerHandler.accept(new RestUpdateSettingsAction(settings, restController));
registerHandler.accept(new RestGetSettingsAction(settings, restController, indexScopedSettings, settingsFilter));
registerHandler.accept(new RestAnalyzeAction(settings, restController));
registerHandler.accept(new RestGetIndexTemplateAction(settings, restController));
registerHandler.accept(new RestPutIndexTemplateAction(settings, restController));
registerHandler.accept(new RestDeleteIndexTemplateAction(settings, restController));
registerHandler.accept(new RestPutMappingAction(settings, restController));
registerHandler.accept(new RestGetMappingAction(settings, restController));
registerHandler.accept(new RestGetFieldMappingAction(settings, restController));
registerHandler.accept(new RestRefreshAction(settings, restController));
registerHandler.accept(new RestFlushAction(settings, restController));
registerHandler.accept(new RestSyncedFlushAction(settings, restController));
registerHandler.accept(new RestForceMergeAction(settings, restController));
registerHandler.accept(new RestUpgradeAction(settings, restController));
registerHandler.accept(new RestClearIndicesCacheAction(settings, restController));
registerHandler.accept(new RestIndexAction(settings, restController));
registerHandler.accept(new RestGetAction(settings, restController));
registerHandler.accept(new RestGetSourceAction(settings, restController));
registerHandler.accept(new RestMultiGetAction(settings, restController));
registerHandler.accept(new RestDeleteAction(settings, restController));
registerHandler.accept(new org.elasticsearch.rest.action.document.RestCountAction(settings, restController));
registerHandler.accept(new RestTermVectorsAction(settings, restController));
registerHandler.accept(new RestMultiTermVectorsAction(settings, restController));
registerHandler.accept(new RestBulkAction(settings, restController));
registerHandler.accept(new RestUpdateAction(settings, restController));
registerHandler.accept(new RestSearchAction(settings, restController));
registerHandler.accept(new RestSearchScrollAction(settings, restController));
registerHandler.accept(new RestClearScrollAction(settings, restController));
registerHandler.accept(new RestMultiSearchAction(settings, restController));
registerHandler.accept(new RestValidateQueryAction(settings, restController));
registerHandler.accept(new RestExplainAction(settings, restController));
registerHandler.accept(new RestRecoveryAction(settings, restController));
// Scripts API
registerHandler.accept(new RestGetStoredScriptAction(settings, restController));
registerHandler.accept(new RestPutStoredScriptAction(settings, restController));
registerHandler.accept(new RestDeleteStoredScriptAction(settings, restController));
registerHandler.accept(new RestFieldStatsAction(settings, restController));
// Tasks API
registerHandler.accept(new RestListTasksAction(settings, restController, nodesInCluster));
registerHandler.accept(new RestGetTaskAction(settings, restController));
registerHandler.accept(new RestCancelTasksAction(settings, restController, nodesInCluster));
// Ingest API
registerHandler.accept(new RestPutPipelineAction(settings, restController));
registerHandler.accept(new RestGetPipelineAction(settings, restController));
registerHandler.accept(new RestDeletePipelineAction(settings, restController));
registerHandler.accept(new RestSimulatePipelineAction(settings, restController));
// CAT API
registerHandler.accept(new RestAllocationAction(settings, restController));
registerHandler.accept(new RestShardsAction(settings, restController));
registerHandler.accept(new RestMasterAction(settings, restController));
registerHandler.accept(new RestNodesAction(settings, restController));
registerHandler.accept(new RestTasksAction(settings, restController, nodesInCluster));
registerHandler.accept(new RestIndicesAction(settings, restController, indexNameExpressionResolver));
registerHandler.accept(new RestSegmentsAction(settings, restController));
// Fully qualified to prevent interference with rest.action.count.RestCountAction
registerHandler.accept(new org.elasticsearch.rest.action.cat.RestCountAction(settings, restController));
// Fully qualified to prevent interference with rest.action.indices.RestRecoveryAction
registerHandler.accept(new org.elasticsearch.rest.action.cat.RestRecoveryAction(settings, restController));
registerHandler.accept(new RestHealthAction(settings, restController));
registerHandler.accept(new org.elasticsearch.rest.action.cat.RestPendingClusterTasksAction(settings, restController));
registerHandler.accept(new RestAliasAction(settings, restController));
registerHandler.accept(new RestThreadPoolAction(settings, restController));
registerHandler.accept(new RestPluginsAction(settings, restController));
registerHandler.accept(new RestFielddataAction(settings, restController));
registerHandler.accept(new RestNodeAttrsAction(settings, restController));
registerHandler.accept(new RestRepositoriesAction(settings, restController));
registerHandler.accept(new RestSnapshotAction(settings, restController));
registerHandler.accept(new RestTemplatesAction(settings, restController));
for (ActionPlugin plugin : actionPlugins) {
for (RestHandler handler : plugin.getRestHandlers(settings, restController, clusterSettings, indexScopedSettings, settingsFilter, indexNameExpressionResolver, nodesInCluster)) {
registerHandler.accept(handler);
}
}
registerHandler.accept(new RestCatAction(settings, restController, catActions));
}
use of org.elasticsearch.rest.RestController in project elasticsearch by elastic.
the class ActionModuleTests method testPluginCantOverwriteBuiltinRestHandler.
public void testPluginCantOverwriteBuiltinRestHandler() throws IOException {
ActionPlugin dupsMainAction = new ActionPlugin() {
@Override
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
return singletonList(new RestMainAction(settings, restController));
}
};
SettingsModule settings = new SettingsModule(Settings.EMPTY);
ThreadPool threadPool = new TestThreadPool(getTestName());
try {
ActionModule actionModule = new ActionModule(false, settings.getSettings(), new IndexNameExpressionResolver(Settings.EMPTY), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(dupsMainAction), null, null);
Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.initRestHandlers(null));
assertThat(e.getMessage(), startsWith("Path [/] already has a value [" + RestMainAction.class.getName()));
} finally {
threadPool.shutdown();
}
}
use of org.elasticsearch.rest.RestController in project elasticsearch by elastic.
the class ActionModuleTests method testPluginCanRegisterRestHandler.
public void testPluginCanRegisterRestHandler() {
class FakeHandler implements RestHandler {
FakeHandler(RestController restController) {
restController.registerHandler(Method.GET, "/_dummy", this);
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
}
}
ActionPlugin registersFakeHandler = new ActionPlugin() {
@Override
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
return singletonList(new FakeHandler(restController));
}
};
SettingsModule settings = new SettingsModule(Settings.EMPTY);
ThreadPool threadPool = new TestThreadPool(getTestName());
try {
ActionModule actionModule = new ActionModule(false, settings.getSettings(), new IndexNameExpressionResolver(Settings.EMPTY), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(registersFakeHandler), null, null);
actionModule.initRestHandlers(null);
// At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(Method.GET, "/_dummy", null));
assertThat(e.getMessage(), startsWith("Path [/_dummy] already has a value [" + FakeHandler.class.getName()));
} finally {
threadPool.shutdown();
}
}
use of org.elasticsearch.rest.RestController in project elasticsearch by elastic.
the class RestIndicesActionTests method testBuildTable.
public void testBuildTable() {
final Settings settings = Settings.EMPTY;
final RestController restController = new RestController(settings, Collections.emptySet(), null, null, null);
final RestIndicesAction action = new RestIndicesAction(settings, restController, new IndexNameExpressionResolver(settings));
// build a (semi-)random table
final int numIndices = randomIntBetween(0, 5);
Index[] indices = new Index[numIndices];
for (int i = 0; i < numIndices; i++) {
indices[i] = new Index(randomAsciiOfLength(5), UUIDs.randomBase64UUID());
}
final MetaData.Builder metaDataBuilder = MetaData.builder();
for (final Index index : indices) {
metaDataBuilder.put(IndexMetaData.builder(index.getName()).settings(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID())).creationDate(System.currentTimeMillis()).numberOfShards(1).numberOfReplicas(1).state(IndexMetaData.State.OPEN));
}
final MetaData metaData = metaDataBuilder.build();
final ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).build();
final String[] indicesStr = new String[indices.length];
for (int i = 0; i < indices.length; i++) {
indicesStr[i] = indices[i].getName();
}
final ClusterHealthResponse clusterHealth = new ClusterHealthResponse(clusterState.getClusterName().value(), indicesStr, clusterState, 0, 0, 0, TimeValue.timeValueMillis(1000L));
final Table table = action.buildTable(new FakeRestRequest(), indices, clusterHealth, randomIndicesStatsResponse(indices), metaData);
// now, verify the table is correct
int count = 0;
List<Table.Cell> headers = table.getHeaders();
assertThat(headers.get(count++).value, equalTo("health"));
assertThat(headers.get(count++).value, equalTo("status"));
assertThat(headers.get(count++).value, equalTo("index"));
assertThat(headers.get(count++).value, equalTo("uuid"));
List<List<Table.Cell>> rows = table.getRows();
assertThat(rows.size(), equalTo(indices.length));
// TODO: more to verify (e.g. randomize cluster health, num primaries, num replicas, etc)
for (int i = 0; i < rows.size(); i++) {
count = 0;
final List<Table.Cell> row = rows.get(i);
// all are red because cluster state doesn't have routing entries
assertThat(row.get(count++).value, equalTo("red*"));
// all are OPEN for now
assertThat(row.get(count++).value, equalTo("open"));
assertThat(row.get(count++).value, equalTo(indices[i].getName()));
assertThat(row.get(count++).value, equalTo(indices[i].getUUID()));
}
}
Aggregations