/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets 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 io.druid.server.log;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import io.druid.java.util.common.concurrent.ScheduledExecutors;
import io.druid.java.util.common.guava.CloseQuietly;
import io.druid.java.util.common.lifecycle.LifecycleStart;
import io.druid.java.util.common.lifecycle.LifecycleStop;
import io.druid.server.RequestLogLine;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.MutableDateTime;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;

/**
 */
public class FileRequestLogger implements RequestLogger
{
  private final ObjectMapper objectMapper;
  private final ScheduledExecutorService exec;
  private final File baseDir;

  private final Object lock = new Object();

  private DateTime currentDay;
  private OutputStreamWriter fileWriter;

  public FileRequestLogger(ObjectMapper objectMapper, ScheduledExecutorService exec, File baseDir)
  {
    this.exec = exec;
    this.objectMapper = objectMapper;
    this.baseDir = baseDir;
  }

  @LifecycleStart
  public void start()
  {
    try {
      baseDir.mkdirs();

      MutableDateTime mutableDateTime = new DateTime().toMutableDateTime();
      mutableDateTime.setMillisOfDay(0);
      synchronized (lock) {
        currentDay = mutableDateTime.toDateTime();

        fileWriter = new OutputStreamWriter(
            new FileOutputStream(new File(baseDir, currentDay.toString("yyyy-MM-dd'.log'")), true),
            Charsets.UTF_8
        );
      }
      long nextDay = currentDay.plusDays(1).getMillis();
      Duration delay = new Duration(nextDay - new DateTime().getMillis());

      ScheduledExecutors.scheduleWithFixedDelay(
          exec,
          delay,
          Duration.standardDays(1),
          new Callable<ScheduledExecutors.Signal>()
          {
            @Override
            public ScheduledExecutors.Signal call()
            {
              try {
                synchronized (lock) {
                  currentDay = currentDay.plusDays(1);
                  CloseQuietly.close(fileWriter);
                  fileWriter = new OutputStreamWriter(
                      new FileOutputStream(new File(baseDir, currentDay.toString()), true),
                      Charsets.UTF_8
                  );
                }
              }
              catch (Exception e) {
                Throwables.propagate(e);
              }

              return ScheduledExecutors.Signal.REPEAT;
            }
          }
      );
    }
    catch (IOException e) {
      Throwables.propagate(e);
    }
  }

  @LifecycleStop
  public void stop()
  {
    synchronized (lock) {
      CloseQuietly.close(fileWriter);
    }
  }

  @Override
  public void log(RequestLogLine requestLogLine) throws IOException
  {
    synchronized (lock) {
      fileWriter.write(
          String.format("%s%n", requestLogLine.getLine(objectMapper))
      );
      fileWriter.flush();
    }
  }
}

@@ -1355,24 +1355,62 @@
 va.io.File;%0A
+import java.io.FileNotFoundException;%0A
 import java.
@@ -1430,16 +1430,16 @@
 Stream;%0A
-
 import j
@@ -2388,167 +2388,22 @@
 r = 
-new OutputStreamWriter(%0A            new FileOutputStream(new File(baseDir, currentDay.toString(%22yyyy-MM-dd'.log'%22)), true),%0A            Charsets.UTF_8%0A        
+getFileWriter(
 );%0A 
@@ -3010,179 +3010,22 @@
 r = 
-new OutputStreamWriter(%0A                      new FileOutputStream(new File(baseDir, currentDay.toString()), true),%0A                      Charsets.UTF_8%0A                  
+getFileWriter(
 );%0A 
@@ -3302,24 +3302,24 @@
 opagate(e);%0A
-
     %7D%0A  %7D%0A%0A 
@@ -3313,24 +3313,274 @@
 %0A    %7D%0A  %7D%0A%0A
+  private OutputStreamWriter getFileWriter() throws FileNotFoundException %7B%0A    return new OutputStreamWriter(%0A            new FileOutputStream(new File(baseDir, currentDay.toString(%22yyyy-MM-dd'.log'%22)), true),%0A            Charsets.UTF_8%0A    );%0A  %7D%0A%0A
   @Lifecycle

Fix rolling of request log files. (#3916)