Skip to content

Commit

Permalink
[shell,core] move alot of shell into core
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog authored and John-John Tedro committed Sep 16, 2015
1 parent 63a5f8e commit 04f9896
Show file tree
Hide file tree
Showing 36 changed files with 426 additions and 457 deletions.
19 changes: 19 additions & 0 deletions heroic-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<!-- used for shell -->
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
<version>2.0.29</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.2</version>
</dependency>

<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>2.12</version>
</dependency>

<!-- used for logging -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.spotify.heroic.shell;


public abstract class AbstractShellTask implements ShellTask {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.spotify.heroic.shell;

import java.io.PrintWriter;
import java.util.Map;
import java.util.SortedMap;

import com.spotify.heroic.shell.ShellTasks.Task;

public interface HeroicShellBridge {
void internalTimeoutTask(PrintWriter out, String[] args);

void exit();

public abstract void printTasksHelp(final PrintWriter out, final SortedMap<String, ShellTasks.Task> tasks);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import java.io.PrintWriter;

import com.spotify.heroic.HeroicCore;

import eu.toolchain.async.AsyncFuture;


public interface ShellTask {
public ShellTaskParams params();

public void standaloneConfig(HeroicCore.Builder builder, ShellTaskParams params);

public AsyncFuture<Void> run(PrintWriter out, ShellTaskParams params) throws Exception;
}
203 changes: 203 additions & 0 deletions heroic-core/src/main/java/com/spotify/heroic/shell/ShellTasks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package com.spotify.heroic.shell;

import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;

import jline.console.ConsoleReader;

import com.google.inject.Inject;
import com.spotify.heroic.HeroicCore;
import com.spotify.heroic.shell.task.ConfigGet;
import com.spotify.heroic.shell.task.Configure;
import com.spotify.heroic.shell.task.Fetch;
import com.spotify.heroic.shell.task.Keys;
import com.spotify.heroic.shell.task.ListBackends;
import com.spotify.heroic.shell.task.MetadataCount;
import com.spotify.heroic.shell.task.MetadataDelete;
import com.spotify.heroic.shell.task.MetadataEntries;
import com.spotify.heroic.shell.task.MetadataFetch;
import com.spotify.heroic.shell.task.MetadataLoad;
import com.spotify.heroic.shell.task.MetadataMigrate;
import com.spotify.heroic.shell.task.MetadataMigrateSuggestions;
import com.spotify.heroic.shell.task.MetadataTags;
import com.spotify.heroic.shell.task.Query;
import com.spotify.heroic.shell.task.RepairNetworkMetadata;
import com.spotify.heroic.shell.task.SuggestKey;
import com.spotify.heroic.shell.task.SuggestPerformance;
import com.spotify.heroic.shell.task.SuggestTag;
import com.spotify.heroic.shell.task.SuggestTagKeyCount;
import com.spotify.heroic.shell.task.SuggestTagValue;
import com.spotify.heroic.shell.task.SuggestTagValues;
import com.spotify.heroic.shell.task.Write;
import com.spotify.heroic.shell.task.WritePerformance;

import eu.toolchain.async.AsyncFramework;
import eu.toolchain.async.AsyncFuture;

public class ShellTasks {
static final Map<String, TaskDefinition> available = new HashMap<>();

static {
// built-in
available.put("exit", builtin("exit shell", (shell, reader, out, tasks, args) -> shell.exit()));
available.put("help",
builtin("print help", (shell, reader, out, tasks, args) -> shell.printTasksHelp(out, tasks)));
available.put(
"timeout",
builtin("get/set shell timeout",
(shell, reader, out, tasks, args) -> shell.internalTimeoutTask(out, args)));
available
.put("clear", builtin("clear shell screen", (shell, reader, out, tasks, args) -> reader.clearScreen()));

available.put("configure", shellTask(Configure.class));
available.put("get", shellTask(ConfigGet.class));
available.put("keys", shellTask(Keys.class));
available.put("backends", shellTask(ListBackends.class));
available.put("fetch", shellTask(Fetch.class));
available.put("write", shellTask(Write.class));
available.put("write-performance", shellTask(WritePerformance.class));
available.put("metadata-delete", shellTask(MetadataDelete.class));
available.put("metadata-fetch", shellTask(MetadataFetch.class));
available.put("metadata-tags", shellTask(MetadataTags.class));
available.put("metadata-count", shellTask(MetadataCount.class));
available.put("metadata-entries", shellTask(MetadataEntries.class));
available.put("metadata-migrate", shellTask(MetadataMigrate.class));
available.put("metadata-migrate-suggestions", shellTask(MetadataMigrateSuggestions.class));
available.put("metadata-load", shellTask(MetadataLoad.class));
available.put("suggest-tag", shellTask(SuggestTag.class));
available.put("suggest-key", shellTask(SuggestKey.class));
available.put("suggest-tag-value", shellTask(SuggestTagValue.class));
available.put("suggest-tag-values", shellTask(SuggestTagValues.class));
available.put("suggest-tag-key-count", shellTask(SuggestTagKeyCount.class));
available.put("suggest-performance", shellTask(SuggestPerformance.class));
available.put("repair-network-metadata", shellTask(RepairNetworkMetadata.class));
available.put("query", shellTask(Query.class));
}

public static Map<String, TaskDefinition> available() {
return available;
}

static TaskDefinition builtin(final String usage, final Builtin command) {
return new TaskDefinition() {
@Override
public TaskShellDefinition setup(HeroicCore core) throws Exception {
return new TaskShellDefinition() {
@Override
public Task setup(final HeroicShellBridge shell, final ConsoleReader reader,
final SortedMap<String, Task> tasks) {
return core.inject(new Task() {
@Inject
AsyncFramework async;

@Override
public ShellTaskParams params() {
return null;
}

@Override
public String usage() {
return usage;
}

@Override
public AsyncFuture<Void> run(PrintWriter out, String[] args, ShellTaskParams params)
throws Exception {
try {
command.run(shell, reader, out, tasks, args);
} catch (Exception e) {
return async.failed(e);
}

return async.resolved();
}
});
}
};
}
};
}

static TaskDefinition shellTask(final Class<? extends ShellTask> task) {
return new TaskDefinition() {
@Override
public TaskShellDefinition setup(final HeroicCore core) throws Exception {
final ShellTask instance = core.inject(instance(task));

final String usage;

final ShellTaskUsage u = task.getAnnotation(ShellTaskUsage.class);

if (u == null) {
usage = String.format("<no @ShellTaskUsage annotation for %s>", task.getName());
} else {
usage = u.value();
}

return new TaskShellDefinition() {
@Override
public Task setup(HeroicShellBridge shell, ConsoleReader reader, SortedMap<String, Task> tasks) {
return new Task() {
@Override
public ShellTaskParams params() {
return instance.params();
}

@Override
public String usage() {
return usage;
}

@Override
public AsyncFuture<Void> run(PrintWriter out, String args[], ShellTaskParams params)
throws Exception {
return instance.run(out, params);
}
};
}
};
};
};
}

public static ShellTask instance(Class<? extends ShellTask> taskType) throws Exception {
final Constructor<? extends ShellTask> constructor;

try {
constructor = taskType.getConstructor();
} catch (ReflectiveOperationException e) {
throw new Exception("Task '" + taskType.getCanonicalName()
+ "' does not have an accessible, empty constructor", e);
}

try {
return constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new Exception("Failed to invoke constructor of '" + taskType.getCanonicalName(), e);
}
}

public static interface Builtin {
void run(HeroicShellBridge shell, ConsoleReader reader, PrintWriter out, SortedMap<String, Task> tasks,
String[] args) throws Exception;
}

public static interface TaskDefinition {
TaskShellDefinition setup(HeroicCore core) throws Exception;
}

public static interface TaskShellDefinition {
Task setup(HeroicShellBridge shell, ConsoleReader reader, SortedMap<String, Task> tasks);
}

public static interface Task {
ShellTaskParams params();

String usage();

AsyncFuture<Void> run(PrintWriter out, String[] args, ShellTaskParams params) throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package com.spotify.heroic.shell;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand All @@ -34,24 +33,11 @@
import org.joda.time.format.DateTimeParser;
import org.joda.time.format.DateTimeParserBucket;

import com.google.common.collect.ImmutableList;
import com.spotify.heroic.HeroicConfig;
import com.spotify.heroic.HeroicCore;
import com.spotify.heroic.HeroicProfile;
import com.spotify.heroic.common.DateRange;
import com.spotify.heroic.common.RangeFilter;
import com.spotify.heroic.elasticsearch.ManagedConnectionFactory;
import com.spotify.heroic.elasticsearch.TransportClientSetup;
import com.spotify.heroic.elasticsearch.index.SingleIndexMapping;
import com.spotify.heroic.filter.Filter;
import com.spotify.heroic.filter.FilterFactory;
import com.spotify.heroic.grammar.QueryParser;
import com.spotify.heroic.metadata.MetadataManagerModule;
import com.spotify.heroic.metadata.MetadataModule;
import com.spotify.heroic.metadata.elasticsearch.ElasticsearchMetadataModule;
import com.spotify.heroic.suggest.SuggestManagerModule;
import com.spotify.heroic.suggest.SuggestModule;
import com.spotify.heroic.suggest.elasticsearch.ElasticsearchSuggestModule;

public final class Tasks {
public static Filter setupFilter(FilterFactory filters, QueryParser parser, QueryParams params) {
Expand Down Expand Up @@ -83,68 +69,6 @@ public static RangeFilter setupRangeFilter(FilterFactory filters, QueryParser pa
return new RangeFilter(filter, params.getRange(), params.getLimit());
}

public static void standaloneElasticsearchConfig(HeroicCore.Builder builder, ElasticSearchParams params) {
final List<String> seeds = Arrays.asList(StringUtils.split(params.getSeeds(), ','));

final String clusterName = params.getClusterName();
final String backendType = params.getBackendType();

builder.profile(new HeroicProfile() {
@Override
public HeroicConfig build() throws Exception {
// @formatter:off

final TransportClientSetup clientSetup = TransportClientSetup.builder()
.clusterName(clusterName)
.seeds(seeds)
.build();

return HeroicConfig.builder()
.metadata(
MetadataManagerModule.builder()
.backends(
ImmutableList.<MetadataModule>of(
ElasticsearchMetadataModule.builder()
.connection(setupConnection(clientSetup, "metadata"))
.writesPerSecond(0d)
.build()
)
).build()
)
.suggest(
SuggestManagerModule.builder()
.backends(
ImmutableList.<SuggestModule>of(
ElasticsearchSuggestModule.builder()
.connection(setupConnection(clientSetup, "suggest"))
.writesPerSecond(0d)
.backendType(backendType)
.build()
)
)
.build()
)

.build();
// @formatter:on
}

private ManagedConnectionFactory setupConnection(TransportClientSetup clientSetup, final String index) {
// @formatter:off
return ManagedConnectionFactory.builder()
.clientSetup(clientSetup)
.index(SingleIndexMapping.builder().index(index).build())
.build();
// @formatter:on
}

@Override
public String description() {
return "load metadata form a file";
}
});
}

private static final List<DateTimeParser> today = new ArrayList<>();
private static final List<DateTimeParser> full = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.spotify.heroic.HeroicConfig;
import com.spotify.heroic.HeroicShell;
import com.spotify.heroic.shell.AbstractShellTask;
import com.spotify.heroic.shell.AbstractShellTaskParams;
import com.spotify.heroic.shell.ShellTaskParams;
Expand All @@ -47,10 +46,6 @@

@ShellTaskUsage("Load metadata from a file")
public class ConfigGet extends AbstractShellTask {
public static void main(String argv[]) throws Exception {
HeroicShell.standalone(argv, ConfigGet.class);
}

@Inject
private HeroicConfig config;

Expand Down
Loading

0 comments on commit 04f9896

Please sign in to comment.