From 84b77c567e6bd6fd2c8a572e10ed5d1513961b94 Mon Sep 17 00:00:00 2001 From: Chris Larsen Date: Sun, 8 Oct 2017 16:00:57 -0700 Subject: [PATCH] Fix #1059 by enabling standard APIs for all roles (including api/stats) and then using the new OperationMode to determine what APIs should be enabled. Signed-off-by: Chris Larsen --- src/tsd/RpcHandler.java | 3 +- src/tsd/RpcManager.java | 118 +++++++++++++++++++++++------------ test/tsd/TestRpcManager.java | 4 ++ 3 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/tsd/RpcHandler.java b/src/tsd/RpcHandler.java index 69224ca8b5..0a89c99e0e 100644 --- a/src/tsd/RpcHandler.java +++ b/src/tsd/RpcHandler.java @@ -91,9 +91,8 @@ public RpcHandler(final TSDB tsdb, final RpcManager manager) { this.rpc_manager = manager; final String cors = tsdb.getConfig().getString("tsd.http.request.cors_domains"); - final String mode = tsdb.getConfig().getString("tsd.mode"); - LOG.info("TSD is in " + mode + " mode"); + LOG.info("TSD is in " + tsdb.getMode() + " mode"); if (cors == null || cors.isEmpty()) { cors_domains = null; diff --git a/src/tsd/RpcManager.java b/src/tsd/RpcManager.java index 7a7dfb16b7..d757d686dc 100644 --- a/src/tsd/RpcManager.java +++ b/src/tsd/RpcManager.java @@ -43,6 +43,7 @@ import net.opentsdb.tools.BuildData; import net.opentsdb.core.Aggregators; import net.opentsdb.core.TSDB; +import net.opentsdb.core.TSDB.OperationMode; import net.opentsdb.query.filter.TagVFilter; import net.opentsdb.stats.StatsCollector; import net.opentsdb.utils.Config; @@ -132,8 +133,7 @@ public static synchronized RpcManager instance(final TSDB tsdb) { } final RpcManager manager = new RpcManager(tsdb); - final String mode = Strings.nullToEmpty(tsdb.getConfig().getString("tsd.mode")); - + // Load any plugins that are enabled via Config. Fail if any plugin cannot be loaded. final ImmutableList.Builder rpcBuilder = ImmutableList.builder(); @@ -145,14 +145,14 @@ public static synchronized RpcManager instance(final TSDB tsdb) { final ImmutableMap.Builder telnetBuilder = ImmutableMap.builder(); final ImmutableMap.Builder httpBuilder = ImmutableMap.builder(); - manager.initializeBuiltinRpcs(mode, telnetBuilder, httpBuilder); + manager.initializeBuiltinRpcs(tsdb.getMode(), telnetBuilder, httpBuilder); manager.telnet_commands = telnetBuilder.build(); manager.http_commands = httpBuilder.build(); final ImmutableMap.Builder httpPluginsBuilder = ImmutableMap.builder(); if (tsdb.getConfig().hasProperty("tsd.http.rpc.plugins")) { final String[] plugins = tsdb.getConfig().getString("tsd.http.rpc.plugins").split(","); - manager.initializeHttpRpcPlugins(mode, plugins, httpPluginsBuilder); + manager.initializeHttpRpcPlugins(tsdb.getMode(), plugins, httpPluginsBuilder); } manager.http_plugin_commands = httpPluginsBuilder.build(); @@ -248,7 +248,7 @@ boolean isHttpRpcPluginPath(final String uri) { * instances. * @param http a map of API endpoints to {@link HttpRpc} instances. */ - private void initializeBuiltinRpcs(final String mode, + private void initializeBuiltinRpcs(final OperationMode mode, final ImmutableMap.Builder telnet, final ImmutableMap.Builder http) { @@ -258,64 +258,100 @@ private void initializeBuiltinRpcs(final String mode, LOG.info("Mode: {}, HTTP UI Enabled: {}, HTTP API Enabled: {}", mode, enableUi, enableApi); - if (mode.equals("rw") || mode.equals("wo")) { - final PutDataPointRpc put = new PutDataPointRpc(tsdb.getConfig()); - final RollupDataPointRpc rollups = new RollupDataPointRpc(tsdb.getConfig()); - final HistogramDataPointRpc histos = new HistogramDataPointRpc(tsdb.getConfig()); + // defaults common to every mode + final StatsRpc stats = new StatsRpc(); + final ListAggregators aggregators = new ListAggregators(); + final DropCachesRpc dropcaches = new DropCachesRpc(); + final Version version = new Version(); + + telnet.put("stats", stats); + telnet.put("dropcaches", dropcaches); + telnet.put("version", version); + telnet.put("exit", new Exit()); + telnet.put("help", new Help()); + + if (enableUi) { + http.put("aggregators", aggregators); + http.put("logs", new LogsRpc()); + http.put("stats", stats); + http.put("version", version); + } + + if (enableApi) { + http.put("api/aggregators", aggregators); + http.put("api/config", new ShowConfig()); + http.put("api/dropcaches", dropcaches); + http.put("api/stats", stats); + http.put("api/version", version); + } + + final PutDataPointRpc put = new PutDataPointRpc(tsdb.getConfig()); + final RollupDataPointRpc rollups = new RollupDataPointRpc(tsdb.getConfig()); + final HistogramDataPointRpc histos = new HistogramDataPointRpc(tsdb.getConfig()); + final SuggestRpc suggest_rpc = new SuggestRpc(); + final AnnotationRpc annotation_rpc = new AnnotationRpc(); + final StaticFileRpc staticfile = new StaticFileRpc(); + + switch(mode) { + case WRITEONLY: telnet.put("put", put); telnet.put("rollup", rollups); telnet.put("histogram", histos); + if (enableApi) { + http.put("api/annotation", annotation_rpc); + http.put("api/annotations", annotation_rpc); http.put("api/put", put); http.put("api/rollup", rollups); http.put("api/histogram", histos); + http.put("api/tree", new TreeRpc()); + http.put("api/uid", new UniqueIdRpc()); } - } - - if (mode.equals("rw") || mode.equals("ro")) { - final StaticFileRpc staticfile = new StaticFileRpc(); - final StatsRpc stats = new StatsRpc(); - final DropCachesRpc dropcaches = new DropCachesRpc(); - final ListAggregators aggregators = new ListAggregators(); - final SuggestRpc suggest_rpc = new SuggestRpc(); - final AnnotationRpc annotation_rpc = new AnnotationRpc(); - final Version version = new Version(); - - telnet.put("stats", stats); - telnet.put("dropcaches", dropcaches); - telnet.put("version", version); - telnet.put("exit", new Exit()); - telnet.put("help", new Help()); - + break; + case READONLY: if (enableUi) { http.put("", new HomePage()); - http.put("aggregators", aggregators); - http.put("dropcaches", dropcaches); + http.put("s", staticfile); http.put("favicon.ico", staticfile); - http.put("logs", new LogsRpc()); + http.put("suggest", suggest_rpc); http.put("q", new GraphHandler()); + } + + if (enableApi) { + http.put("api/query", new QueryRpc()); + http.put("api/search", new SearchRpc()); + http.put("api/suggest", suggest_rpc); + } + + break; + case READWRITE: + telnet.put("put", put); + telnet.put("rollup", rollups); + telnet.put("histogram", histos); + + if (enableUi) { + http.put("", new HomePage()); http.put("s", staticfile); - http.put("stats", stats); + http.put("favicon.ico", staticfile); http.put("suggest", suggest_rpc); - http.put("version", version); + http.put("q", new GraphHandler()); } - + if (enableApi) { - http.put("api/aggregators", aggregators); - http.put("api/annotation", annotation_rpc); - http.put("api/annotations", annotation_rpc); - http.put("api/config", new ShowConfig()); - http.put("api/dropcaches", dropcaches); http.put("api/query", new QueryRpc()); http.put("api/search", new SearchRpc()); - http.put("api/serializers", new Serializers()); - http.put("api/stats", stats); + http.put("api/annotation", annotation_rpc); + http.put("api/annotations", annotation_rpc); http.put("api/suggest", suggest_rpc); + http.put("api/put", put); + http.put("api/rollup", rollups); + http.put("api/histogram", histos); http.put("api/tree", new TreeRpc()); http.put("api/uid", new UniqueIdRpc()); - http.put("api/version", version); } } + + if (enableDieDieDie) { final DieDieDie diediedie = new DieDieDie(); @@ -338,7 +374,7 @@ private void initializeBuiltinRpcs(final String mode, * to {@link HttpRpcPlugin} instance. */ @VisibleForTesting - protected void initializeHttpRpcPlugins(final String mode, + protected void initializeHttpRpcPlugins(final OperationMode mode, final String[] pluginClassNames, final ImmutableMap.Builder http) { for (final String plugin : pluginClassNames) { diff --git a/test/tsd/TestRpcManager.java b/test/tsd/TestRpcManager.java index fc062bc506..e97cadfc06 100644 --- a/test/tsd/TestRpcManager.java +++ b/test/tsd/TestRpcManager.java @@ -29,6 +29,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import net.opentsdb.core.TSDB; +import net.opentsdb.core.TSDB.OperationMode; import net.opentsdb.utils.Config; import net.opentsdb.utils.PluginLoader; @@ -53,6 +54,7 @@ public void before() { when(config.getString("tsd.no_diediedie")) .thenReturn("false"); TSDB tsdb = mock(TSDB.class); + when(tsdb.getMode()).thenReturn(OperationMode.READWRITE); when(tsdb.getConfig()).thenReturn(config); mock_tsdb_no_plugins = tsdb; } @@ -79,6 +81,7 @@ public void loadHttpRpcPlugins() throws Exception { .thenReturn("false"); TSDB tsdb = mock(TSDB.class); + when(tsdb.getMode()).thenReturn(OperationMode.READWRITE); when(tsdb.getConfig()).thenReturn(config); PluginLoader.loadJAR("plugin_test.jar"); @@ -109,6 +112,7 @@ public void loadRpcPlugin() throws Exception { .thenReturn("false"); TSDB tsdb = mock(TSDB.class); + when(tsdb.getMode()).thenReturn(OperationMode.READWRITE); when(tsdb.getConfig()).thenReturn(config); PluginLoader.loadJAR("plugin_test.jar");