Github user tnine commented on a diff in the pull request:
https://github.com/apache/incubator-usergrid/pull/307#discussion_r34699249
--- Diff:
stack/tools/src/main/java/org/apache/usergrid/tools/ExportApp.java ---
@@ -0,0 +1,552 @@
+/*
+ * 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.usergrid.tools;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.usergrid.persistence.Entity;
+import org.apache.usergrid.persistence.EntityManager;
+import org.apache.usergrid.persistence.Query;
+import org.apache.usergrid.persistence.Results;
+import org.apache.usergrid.utils.StringUtils;
+import org.codehaus.jackson.JsonGenerator;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.util.MinimalPrettyPrinter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import rx.Observable;
+import rx.Scheduler;
+import rx.Subscriber;
+import rx.functions.Action0;
+import rx.functions.Action1;
+import rx.functions.Func1;
+import rx.schedulers.Schedulers;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.*;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Export all entities and connections of a Usergrid app.
+ *
+ * Exports data files to specified directory.
+ *
+ * Will create as many output files as there are writeThreads (by default:
10).
+ *
+ * Will create two types of files: *.uge for Usegrird entities and *.ugc
for entity to entity connections.
+ *
+ * Every line of the data files is a complete JSON object.
+ */
+public class ExportApp extends ExportingToolBase {
+ static final Logger logger = LoggerFactory.getLogger( ExportApp.class
);
+
+ static final String APPLICATION_NAME = "application";
+ private static final String READ_THREAD_COUNT = "readThreads";
+ private static final String WRITE_THREAD_COUNT = "writeThreads";
+
+ String applicationName;
+ String organizationName;
+
+ AtomicInteger entitiesWritten = new AtomicInteger(0);
+ AtomicInteger connectionsWritten = new AtomicInteger(0);
+
+ Scheduler readScheduler;
+ Scheduler writeScheduler;
+
+ ObjectMapper mapper = new ObjectMapper();
+ Map<Thread, JsonGenerator> entityGeneratorsByThread = new
HashMap<Thread, JsonGenerator>();
+ Map<Thread, JsonGenerator> connectionGeneratorsByThread = new
HashMap<Thread, JsonGenerator>();
+
+ // set via CLI
+ int readThreadCount = 80;
+ int writeThreadCount = 10; // limiting write will limit output files
+
+
+ @Override
+ @SuppressWarnings("static-access")
+ public Options createOptions() {
+
+ Options options = super.createOptions();
+
+ Option appNameOption = OptionBuilder.hasArg().withType("")
+ .withDescription( "Application Name -" + APPLICATION_NAME
).create( APPLICATION_NAME );
+ options.addOption( appNameOption );
+
+ Option readThreadsOption = OptionBuilder.hasArg().withType(0)
+ .withDescription( "Read Threads -" + READ_THREAD_COUNT
).create( READ_THREAD_COUNT );
+ options.addOption( readThreadsOption );
+
+ Option writeThreadsOption = OptionBuilder.hasArg().withType(0)
+ .withDescription( "Write Threads -" + WRITE_THREAD_COUNT
).create(WRITE_THREAD_COUNT);
+ options.addOption( writeThreadsOption );
+
+ return options;
+ }
+
+
+ /**
+ * Tool entry point.
+ */
+ @Override
+ public void runTool(CommandLine line) throws Exception {
+
+ applicationName = line.getOptionValue( APPLICATION_NAME );
+
+ if (StringUtils.isNotEmpty( line.getOptionValue( READ_THREAD_COUNT
) )) {
+ try {
+ readThreadCount = Integer.parseInt( line.getOptionValue(
READ_THREAD_COUNT ) );
+ } catch (NumberFormatException nfe) {
+ logger.error( "-" + READ_THREAD_COUNT + " must be
specified as an integer. Aborting..." );
+ return;
+ }
+ }
+
+ if (StringUtils.isNotEmpty( line.getOptionValue(
WRITE_THREAD_COUNT ) )) {
+ try {
+ writeThreadCount = Integer.parseInt( line.getOptionValue(
WRITE_THREAD_COUNT ) );
+ } catch (NumberFormatException nfe) {
+ logger.error( "-" + WRITE_THREAD_COUNT + " must be
specified as an integer. Aborting..." );
+ return;
+ }
+ }
+
+ setVerbose( line );
+
+ applyOrgId( line );
+ prepareBaseOutputFileName( line );
+ outputDir = createOutputParentDir();
+ logger.info( "Export directory: " + outputDir.getAbsolutePath() );
+
+ startSpring();
+
+ UUID applicationId = emf.lookupApplication( applicationName );
+ final EntityManager em = emf.getEntityManager( applicationId );
+ organizationName = em.getApplication().getOrganizationName();
+
+ ExecutorService readThreadPoolExecutor =
Executors.newFixedThreadPool( readThreadCount );
+ readScheduler = Schedulers.from( readThreadPoolExecutor );
+
+ ExecutorService writeThreadPoolExecutor =
Executors.newFixedThreadPool( writeThreadCount );
+ writeScheduler = Schedulers.from( writeThreadPoolExecutor );
+
+ Observable<String> collectionsObservable = Observable.create( new
CollectionsObservable( em ) );
+
+ collectionsObservable.flatMap( new Func1<String,
Observable<ExportEntity>>() {
+
+ public Observable<ExportEntity> call(String collection) {
+ return Observable.create( new EntityObservable( em,
collection ))
+ .doOnNext( new EntityWriteAction() ).subscribeOn(
writeScheduler );
+ }
+
+ }, 10).flatMap( new Func1<ExportEntity,
Observable<ExportConnection>>() {
--- End diff --
Rather than have a static 10 here, we should probably use the
Schedulers.io() scheduler, then use the write thread count as this value.
Otherwise you'll just introduce contention on a thread pool. If we use the
unbounded Schedulers.io(), and us the size as the flatmap argument, we'll never
exceed the set thread size.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---