Skip to content

Commit

Permalink
Fix #1059 by enabling standard APIs for all roles (including api/stats)
Browse files Browse the repository at this point in the history
and then using the new OperationMode to determine what APIs should be
enabled.

Signed-off-by: Chris Larsen <[email protected]>
  • Loading branch information
manolama committed Oct 8, 2017
1 parent 47614a5 commit 84b77c5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/tsd/RpcHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
118 changes: 77 additions & 41 deletions src/tsd/RpcManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RpcPlugin> rpcBuilder = ImmutableList.builder();
Expand All @@ -145,14 +145,14 @@ public static synchronized RpcManager instance(final TSDB tsdb) {

final ImmutableMap.Builder<String, TelnetRpc> telnetBuilder = ImmutableMap.builder();
final ImmutableMap.Builder<String, HttpRpc> 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<String, HttpRpcPlugin> 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();

Expand Down Expand Up @@ -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<String, TelnetRpc> telnet,
final ImmutableMap.Builder<String, HttpRpc> http) {

Expand All @@ -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();
Expand All @@ -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<String, HttpRpcPlugin> http) {
for (final String plugin : pluginClassNames) {
Expand Down
4 changes: 4 additions & 0 deletions test/tsd/TestRpcManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 84b77c5

Please sign in to comment.