Skip to content

Commit

Permalink
OAK-6224 - Enable dumping index definitions and stats via oak-run
Browse files Browse the repository at this point in the history
-- Initial setup for index command
-- Added dependency on felix inventory api and redirected
   test output to text file
-- Support for dumping stats and index definitions

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/trunk@1795321 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chetanmeh committed May 16, 2017
1 parent 059bdc3 commit fd627f0
Show file tree
Hide file tree
Showing 8 changed files with 585 additions and 0 deletions.
11 changes: 11 additions & 0 deletions oak-run/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -295,6 +301,11 @@
<artifactId>tika-core</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.inventory</artifactId>
<version>1.0.4</version>
</dependency>

<!-- Findbugs annotations -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.jackrabbit.oak.index;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.apache.felix.inventory.Format;
import org.apache.jackrabbit.oak.console.NodeStoreFixture;
import org.apache.jackrabbit.oak.run.cli.NodeStoreFixtureProvider;
import org.apache.jackrabbit.oak.run.cli.Options;
import org.apache.jackrabbit.oak.run.commons.Command;
import org.apache.jackrabbit.oak.spi.state.NodeStore;

public class IndexCommand implements Command {

public static final String INDEX_DEFINITIONS_JSON = "index-definitions.json";
public static final String INDEX_INFO_TXT = "index-info.txt";
private File info;
private File definitions;

@Override
public void execute(String... args) throws Exception {
OptionParser parser = new OptionParser();

Options opts = new Options();
opts.registerOptionsFactory(IndexOptions.FACTORY);

opts.parseAndConfigure(parser, args);

IndexOptions indexOpts = opts.getOptionBean(IndexOptions.class);

try (NodeStoreFixture fixture = NodeStoreFixtureProvider.create(opts)) {
execute(fixture.getStore(), indexOpts);
tellReportPaths();
}
}

private void tellReportPaths() {
if (info != null) {
System.out.printf("Index stats stored at %s%n", getPath(info));
}

if (definitions != null) {
System.out.printf("Index definitions stored at %s%n", getPath(definitions));
}
}

private void execute(NodeStore store, IndexOptions indexOpts) throws IOException {
IndexHelper indexHelper = new IndexHelper(store, indexOpts.getOutDir(), indexOpts.getWorkDir());

dumpIndexStats(indexOpts, indexHelper);
dumpIndexDefinitions(indexOpts, indexHelper);
}

private void dumpIndexDefinitions(IndexOptions indexOpts, IndexHelper indexHelper) throws IOException {
if (indexOpts.dumpDefinitions()) {
PrinterDumper dumper = new PrinterDumper(indexHelper.getOutputDir(), INDEX_DEFINITIONS_JSON,
false, Format.JSON, indexHelper.getIndexDefnPrinter());
dumper.dump();
definitions = dumper.getOutFile();
}
}

private void dumpIndexStats(IndexOptions indexOpts, IndexHelper indexHelper) throws IOException {
if (indexOpts.dumpStats()) {
PrinterDumper dumper = new PrinterDumper(indexHelper.getOutputDir(), INDEX_INFO_TXT,
true, Format.TEXT, indexHelper.getIndexPrinter());
dumper.dump();
info = dumper.getOutFile();
}
}

private static Path getPath(File file) {
return file.toPath().normalize().toAbsolutePath();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.jackrabbit.oak.index;

import java.io.File;

import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService;
import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoServiceImpl;
import org.apache.jackrabbit.oak.plugins.index.IndexInfoService;
import org.apache.jackrabbit.oak.plugins.index.IndexInfoServiceImpl;
import org.apache.jackrabbit.oak.plugins.index.IndexPathService;
import org.apache.jackrabbit.oak.plugins.index.IndexPathServiceImpl;
import org.apache.jackrabbit.oak.plugins.index.inventory.IndexDefinitionPrinter;
import org.apache.jackrabbit.oak.plugins.index.inventory.IndexPrinter;
import org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexInfoProvider;
import org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexInfoProvider;
import org.apache.jackrabbit.oak.spi.state.NodeStore;

class IndexHelper {
private final NodeStore store;
private final File outputDir;
private final File workDir;
private IndexInfoServiceImpl indexInfoService;
private IndexPathService indexPathService;
private AsyncIndexInfoService asyncIndexInfoService;

IndexHelper(NodeStore store, File outputDir, File workDir) {
this.store = store;
this.outputDir = outputDir;
this.workDir = workDir;
}

public File getOutputDir() {
return outputDir;
}

public IndexPrinter getIndexPrinter() {
return new IndexPrinter(getIndexInfoService(), getAsyncIndexInfoService());
}

public IndexDefinitionPrinter getIndexDefnPrinter() {
return new IndexDefinitionPrinter(store, getIndexPathService());
}

private IndexPathService getIndexPathService() {
if (indexPathService == null) {
indexPathService = new IndexPathServiceImpl(store);
}
return indexPathService;
}

private AsyncIndexInfoService getAsyncIndexInfoService() {
if (asyncIndexInfoService == null) {
asyncIndexInfoService = new AsyncIndexInfoServiceImpl(store);
}
return asyncIndexInfoService;
}

private IndexInfoService getIndexInfoService() {
if (indexInfoService == null) {
indexInfoService = new IndexInfoServiceImpl(store, getIndexPathService());
bindIndexInfoProviders(indexInfoService);
}
return indexInfoService;
}

private void bindIndexInfoProviders(IndexInfoServiceImpl indexInfoService) {
indexInfoService.bindInfoProviders(new LuceneIndexInfoProvider(store, getAsyncIndexInfoService(), workDir));
indexInfoService.bindInfoProviders(new PropertyIndexInfoProvider(store));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.jackrabbit.oak.index;

import java.io.File;
import java.io.IOException;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.oak.run.cli.OptionsBean;
import org.apache.jackrabbit.oak.run.cli.OptionsBeanFactory;

public class IndexOptions implements OptionsBean {

public static final OptionsBeanFactory FACTORY = new OptionsBeanFactory() {
@Override
public OptionsBean newInstance(OptionParser parser) {
return new IndexOptions(parser);
}
};

private final OptionSpec<File> workDirOpt;
private final OptionSpec<File> outputDirOpt;
private final OptionSpec<Void> stats;
private final OptionSpec<Void> definitions;
private OptionSet options;


public IndexOptions(OptionParser parser){
workDirOpt = parser.accepts("index-work-dir", "Work directory used for storing temp files")
.withRequiredArg().ofType(File.class).defaultsTo(new File("target"));
outputDirOpt = parser.accepts("index-out-dir", "Directory used for output files")
.withRequiredArg().ofType(File.class).defaultsTo(new File("."));
stats = parser.accepts("index-info", "Collects and dumps information related to the indexes");
definitions = parser.accepts("index-definitions", "Collects and dumps index definitions");
}

@Override
public void configure(OptionSet options) {
this.options = options;
}

public File getWorkDir() throws IOException {
File workDir = workDirOpt.value(options);
FileUtils.forceMkdir(workDir);
return workDir;
}

public File getOutDir() {
return outputDirOpt.value(options);
}

public boolean dumpStats(){
return options.has(stats);
}

public boolean dumpDefinitions(){
return options.has(definitions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.jackrabbit.oak.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.TeeOutputStream;
import org.apache.felix.inventory.Format;
import org.apache.felix.inventory.InventoryPrinter;

import static com.google.common.base.Preconditions.checkNotNull;

class PrinterDumper {
private final File outDir;
private final String fileName;
private final boolean dumpToSysOut;
private final Format format;
private final InventoryPrinter printer;
private File outFile;

public PrinterDumper(File outDir, String fileName, boolean dumpToSysOut, Format format, InventoryPrinter printer) {
this.outDir = outDir;
this.fileName = fileName;
this.dumpToSysOut = dumpToSysOut;
this.format = format;
this.printer = printer;
}

public void dump() throws IOException {
try (OutputStream os = newOutput()) {
OutputStream writerStream = dumpToSysOut ? new TeeOutputStream(os, System.out) : os;
PrintWriter pw = new PrintWriter(writerStream);
printer.print(pw, format, false);
pw.flush();
}
}

public File getOutFile() {
return checkNotNull(outFile);
}

private OutputStream newOutput() throws IOException {
outFile = new File(outDir, fileName);
return new BufferedOutputStream(FileUtils.openOutputStream(outFile));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.jackrabbit.oak.run;

import com.google.common.collect.ImmutableMap;
import org.apache.jackrabbit.oak.index.IndexCommand;
import org.apache.jackrabbit.oak.run.commons.Command;
import org.apache.jackrabbit.oak.run.commons.Modes;

Expand Down Expand Up @@ -52,5 +53,6 @@ public final class AvailableModes {
.put("tika", new TikaCommand())
.put("upgrade", new UpgradeCommand())
.put("unlockupgrade", new UnlockUpgradeCommand())
.put("index", new IndexCommand())
.build());
}
Loading

0 comments on commit fd627f0

Please sign in to comment.