Github user mikewalch commented on a diff in the pull request:

    
https://github.com/apache/incubator-fluo-recipes/pull/103#discussion_r79475688
  
    --- Diff: 
modules/accumulo/src/main/java/org/apache/fluo/recipes/accumulo/export/AccumuloWriter.java
 ---
    @@ -0,0 +1,310 @@
    +/*
    + * 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.fluo.recipes.accumulo.export;
    +
    +import java.util.ArrayList;
    +import java.util.Collection;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.concurrent.CountDownLatch;
    +import java.util.concurrent.LinkedBlockingQueue;
    +import java.util.function.Predicate;
    +
    +import org.apache.accumulo.core.client.AccumuloException;
    +import org.apache.accumulo.core.client.AccumuloSecurityException;
    +import org.apache.accumulo.core.client.BatchWriter;
    +import org.apache.accumulo.core.client.BatchWriterConfig;
    +import org.apache.accumulo.core.client.ClientConfiguration;
    +import org.apache.accumulo.core.client.Connector;
    +import org.apache.accumulo.core.client.MutationsRejectedException;
    +import org.apache.accumulo.core.client.TableExistsException;
    +import org.apache.accumulo.core.client.TableNotFoundException;
    +import org.apache.accumulo.core.client.ZooKeeperInstance;
    +import org.apache.accumulo.core.client.security.tokens.PasswordToken;
    +import org.apache.accumulo.core.data.Mutation;
    +import org.apache.accumulo.core.security.ColumnVisibility;
    +import org.apache.fluo.api.config.FluoConfiguration;
    +import org.apache.fluo.api.config.SimpleConfiguration;
    +import org.apache.fluo.api.data.Bytes;
    +import org.apache.fluo.api.data.Column;
    +import org.apache.fluo.api.data.RowColumn;
    +import org.apache.fluo.recipes.core.transaction.LogEntry;
    +import org.apache.fluo.recipes.core.transaction.TxLog;
    +
    +/**
    + * Writes mutations to Accumulo using a shared batch writer
    + *
    + * @since 1.0.0
    + */
    +public class AccumuloWriter {
    +
    +  private static class Mutations {
    +    List<Mutation> mutations;
    +    CountDownLatch cdl = new CountDownLatch(1);
    +
    +    Mutations(Collection<Mutation> mutations) {
    +      this.mutations = new ArrayList<>(mutations);
    +    }
    +  }
    +
    +  /**
    +   * Sets AccumuloWriter config in app configuration
    +   */
    +  public static void setConfig(SimpleConfiguration conf, String id, Config 
ti) {
    +    String prefix = "recipes.accumulo.writer." + id;
    +    conf.setProperty(prefix + ".instance", ti.instanceName);
    +    conf.setProperty(prefix + ".zookeepers", ti.zookeepers);
    +    conf.setProperty(prefix + ".user", ti.user);
    +    conf.setProperty(prefix + ".password", ti.password);
    +    conf.setProperty(prefix + ".table", ti.table);
    +  }
    +
    +  /**
    +   * Gets Accumulo Config from app configuration
    +   */
    +  public static Config getConfig(SimpleConfiguration conf, String id) {
    +    String prefix = "recipes.accumulo.writer." + id;
    +    String instanceName = conf.getString(prefix + ".instance");
    +    String zookeepers = conf.getString(prefix + ".zookeepers");
    +    String user = conf.getString(prefix + ".user");
    +    String password = conf.getString(prefix + ".password");
    +    String table = conf.getString(prefix + ".table");
    +    return new Config(instanceName, zookeepers, user, password, table);
    +  }
    +
    +  private static class ExportTask implements Runnable {
    +
    +    private BatchWriter bw;
    +
    +    ExportTask(String instanceName, String zookeepers, String user, String 
password, String table)
    +        throws TableNotFoundException, AccumuloException, 
AccumuloSecurityException {
    +      ZooKeeperInstance zki =
    +          new ZooKeeperInstance(new 
ClientConfiguration().withInstance(instanceName).withZkHosts(
    +              zookeepers));
    +
    +      // TODO need to close batch writer
    +      Connector conn = zki.getConnector(user, new PasswordToken(password));
    +      try {
    +        bw = conn.createBatchWriter(table, new BatchWriterConfig());
    +      } catch (TableNotFoundException tnfe) {
    +        try {
    +          conn.tableOperations().create(table);
    +        } catch (TableExistsException e) {
    +          // nothing to do
    +        }
    +
    +        bw = conn.createBatchWriter(table, new BatchWriterConfig());
    +      }
    +    }
    +
    +    @Override
    +    public void run() {
    +
    +      ArrayList<Mutations> exports = new ArrayList<>();
    +
    +      while (true) {
    +        try {
    +          exports.clear();
    +
    +          // gather export from all threads that have placed an item on 
the queue
    +          exports.add(exportQueue.take());
    +          exportQueue.drainTo(exports);
    +
    +          for (Mutations ml : exports) {
    +            bw.addMutations(ml.mutations);
    +          }
    +
    +          bw.flush();
    +
    +          // notify all threads waiting after flushing
    +          for (Mutations ml : exports) {
    +            ml.cdl.countDown();
    +          }
    +
    +        } catch (InterruptedException | MutationsRejectedException e) {
    +          throw new RuntimeException(e);
    +        }
    +      }
    +    }
    +
    +  }
    +
    +  private static LinkedBlockingQueue<Mutations> exportQueue = null;
    +
    +  private AccumuloWriter(String instanceName, String zookeepers, String 
user, String password,
    +      String table) {
    +
    +    // TODO: fix this write to static and remove findbugs max rank 
override in pom.xml
    +    exportQueue = new LinkedBlockingQueue<>(10000);
    +
    +    try {
    +      Thread queueProcessingTask =
    +          new Thread(new ExportTask(instanceName, zookeepers, user, 
password, table));
    +      queueProcessingTask.setDaemon(true);
    +      queueProcessingTask.start();
    +    } catch (Exception e) {
    +      throw new IllegalStateException(e);
    +    }
    +  }
    +
    +  private static Map<String, AccumuloWriter> exporters = new HashMap<>();
    +
    +  public static AccumuloWriter getInstance(FluoConfiguration config, 
String id) {
    +    return getInstance(config.getAppConfiguration(), id);
    +  }
    +
    +  public static AccumuloWriter getInstance(SimpleConfiguration conf, 
String id) {
    +    return getInstance(getConfig(conf, id));
    +  }
    +
    +  public static AccumuloWriter getInstance(Config ti) {
    +    return getInstance(ti.instanceName, ti.zookeepers, ti.user, 
ti.password, ti.table);
    +  }
    +
    +  public static synchronized AccumuloWriter getInstance(String 
instanceName, String zookeepers,
    +      String user, String password, String table) {
    +
    +    String key =
    +        instanceName + ":" + zookeepers + ":" + user + ":" + 
password.hashCode() + ":" + table;
    +
    +    AccumuloWriter ret = exporters.get(key);
    +
    +    if (ret == null) {
    +      ret = new AccumuloWriter(instanceName, zookeepers, user, password, 
table);
    +      exporters.put(key, ret);
    +    }
    +
    +    return ret;
    +  }
    +
    +  public void write(Collection<Mutation> mutations) {
    +    Mutations work = new Mutations(mutations);
    +    exportQueue.add(work);
    +    try {
    +      work.cdl.await();
    +    } catch (InterruptedException e) {
    +      throw new RuntimeException(e);
    +    }
    +  }
    +
    +  /**
    +   * Returns Replication Filter
    +   */
    +  public static Predicate<LogEntry> getReplicationFilter() {
    +    return le -> le.getOp().equals(LogEntry.Operation.DELETE)
    +        || le.getOp().equals(LogEntry.Operation.SET);
    +  }
    +
    +  /**
    +   * Generates Accumulo mutations from a Transaction log. Used to 
Replicate Fluo table to Accumulo.
    +   *
    +   * @param txLog Transaction log
    +   * @param seq Export sequence number
    +   * @return Collection of mutations
    +   */
    +  public static Collection<Mutation> generateMutations(long seq, TxLog 
txLog) {
    --- End diff --
    
    Yes. I agree.  The `TxLog` code is in fluo-recipes-core but this is here to 
avoid adding accumulo as dependency to core.  I will make a 
`AccumuloReplication` class fluo-recipes-accumulo and add this and the filter 
method above.  Does that work?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to