/*
 * Copyright (C) 2014-2016 LinkedIn Corp. All rights reserved.
 *
 * Licensed 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.
 */

package gobblin.publisher;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.hadoop.fs.Path;

import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import com.google.common.io.Closer;

import gobblin.configuration.ConfigurationKeys;
import gobblin.configuration.State;
import gobblin.configuration.WorkUnitState;
import gobblin.hive.HiveRegProps;
import gobblin.hive.HiveRegister;
import gobblin.hive.policy.HiveRegistrationPolicy;
import gobblin.hive.policy.HiveRegistrationPolicyBase;
import gobblin.hive.spec.HiveSpec;
import gobblin.util.ExecutorsUtils;
import lombok.extern.slf4j.Slf4j;


/**
 * A {@link DataPublisher} that registers the already published data with Hive.
 *
 * <p>
 *   This publisher is not responsible for publishing data, and it relies on another publisher
 *   to document the published paths in property {@link ConfigurationKeys#PUBLISHER_DIRS}. Thus this publisher
 *   should generally be used as a job level data publisher, where the task level publisher should be a publisher
 *   that documents the published paths, such as {@link BaseDataPublisher}.
 * </p>
 *
 * @author Ziyang Liu
 */
@Slf4j
public class HiveRegistrationPublisher extends DataPublisher {

  private final Closer closer = Closer.create();
  private final HiveRegister hiveRegister;
  private final HiveRegistrationPolicy policy;
  private final ExecutorService hivePolicyExecutor;

  public HiveRegistrationPublisher(State state) {
    super(state);
    this.hiveRegister = this.closer.register(HiveRegister.get(state));
    this.policy = HiveRegistrationPolicyBase.getPolicy(state);
    this.hivePolicyExecutor = Executors.newFixedThreadPool(new HiveRegProps(state).getNumThreads(),
        ExecutorsUtils.newThreadFactory(Optional.of(log), Optional.of("HivePolicyExecutor-%d")));
  }

  @Override
  public void close() throws IOException {
    try {
      ExecutorsUtils.shutdownExecutorService(this.hivePolicyExecutor, Optional.of(log));
    } finally {
      this.closer.close();
    }
  }

  @Deprecated
  @Override
  public void initialize() throws IOException {}

  @Override
  public void publishData(Collection<? extends WorkUnitState> states) throws IOException {
    CompletionService<Collection<HiveSpec>> completionService =
        new ExecutorCompletionService<>(this.hivePolicyExecutor);

    Set<String> pathsToRegister = getUniquePathsToRegister(states);
    log.info("Number of paths to be registered in Hive: " + pathsToRegister.size());
    for (final String path : pathsToRegister) {
      completionService.submit(new Callable<Collection<HiveSpec>>() {

        @Override
        public Collection<HiveSpec> call() throws Exception {
          return HiveRegistrationPublisher.this.policy.getHiveSpecs(new Path(path));
        }
      });

    }

    for (int i = 0; i < pathsToRegister.size(); i++) {
      try {
        for (HiveSpec spec : completionService.take().get()) {
          this.hiveRegister.register(spec);
        }
      } catch (InterruptedException | ExecutionException e) {
        log.info("Failed to generate HiveSpec", e);
        throw new IOException(e);
      }
    }
    log.info("Finished generating all HiveSpecs");
  }

  private static Set<String> getUniquePathsToRegister(Collection<? extends WorkUnitState> states) {
    Set<String> paths = Sets.newHashSet();
    for (State state : states) {
      if (state.contains(ConfigurationKeys.PUBLISHER_DIRS)) {
        paths.addAll(state.getPropAsList(ConfigurationKeys.PUBLISHER_DIRS));
      }
    }
    return paths;
  }

  @Override
  public void publishMetadata(Collection<? extends WorkUnitState> states) throws IOException {
    // Nothing to do
  }

}

@@ -886,16 +886,54 @@
 ecutors;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -1450,16 +1450,17 @@
 sUtils;%0A
+%0A
 import l
@@ -2085,16 +2085,89 @@
 sher %7B%0A%0A
+  private static final String DATA_PUBLISH_TIME = %22lastDataPublishTime%22;%0A
   privat
@@ -2254,55 +2254,8 @@
 er;%0A
-  private final HiveRegistrationPolicy policy;%0A
   pr
@@ -2446,71 +2446,8 @@
 ));%0A
-    this.policy = HiveRegistrationPolicyBase.getPolicy(state);%0A
     
@@ -3159,24 +3159,173 @@
 Executor);%0A%0A
+    addRuntimeHiveRegistrationProperties(super.state);%0A    final HiveRegistrationPolicy policy = HiveRegistrationPolicyBase.getPolicy(super.state);%0A%0A
     Set%3CStri
@@ -3685,39 +3685,8 @@
 urn 
-HiveRegistrationPublisher.this.
 poli
@@ -4633,10 +4633,377 @@
 do%0A  %7D%0A%0A
+  private static void addRuntimeHiveRegistrationProperties(State state) %7B%0A    // Use seconds instead of milliseconds to be consistent with other times stored in hive%0A    state.appendToListProp(HiveRegProps.HIVE_TABLE_PARTITION_PROPS,%0A        String.format(%22%25s:%25d%22, DATA_PUBLISH_TIME, TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS)));%0A  %7D%0A
 %7D%0A

Add lastDataPublishTime in hive table/partition properties