All Downloads are FREE. Search and download functionalities are using the official Maven repository.

water.util.GetLogsFromNode Maven / Gradle / Ivy

There is a newer version: 3.8.2.9
Show newest version
package water.util;

import water.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Get zipped log directory data from a node.
 * The intent here is to return a binary blob with the data for saving as a file.
 * This is used as part of the "/3/Logs/download" REST API.
 */
public class GetLogsFromNode extends Iced {
  static final int MB = 1 << 20;
  static final int MAX_SIZE = 25 * MB;

  // Input
  /**
   * Node number to get logs from (starting at 0).
   */
  public int nodeidx;

  // Output
  /**
   * Byte array containing a zipped file with the entire log directory.
   */
  public byte[] bytes;

  /**
   * Do the work.
   */
  public void doIt() {
    if (nodeidx == -1) {
      GetLogsTask t = new GetLogsTask();
      t.doIt();
      bytes = t._bytes;
    }
    else {
      H2ONode node = H2O.CLOUD._memary[nodeidx];
      GetLogsTask t = new GetLogsTask();
      Log.trace("GetLogsTask starting to node " + nodeidx + "...");
      // Synchronous RPC call to get ticks from remote (possibly this) node.
      new RPC<>(node, t).call().get();
      Log.trace("GetLogsTask completed to node " + nodeidx);
      bytes = t._bytes;
    }
  }

  private static class GetLogsTask extends DTask {
    private byte[] _bytes;

    public GetLogsTask() { super(H2O.MIN_HI_PRIORITY); _bytes = null; }

    public void doIt() {
      try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(baos);
        zipDir(Log.LOG_DIR, baos, zos);
        zos.close();
        baos.close();
        _bytes = baos.toByteArray();
      }
      catch (Exception e) {
        _bytes = e.toString().getBytes();
      }
    }

    @Override public void compute2() {
      doIt();
      tryComplete();
    }

    //here is the code for the method
    private void zipDir(String dir2zip, ByteArrayOutputStream baos, ZipOutputStream zos) throws IOException
    {
      try
      {
        //create a new File object based on the directory we have to zip.
        File zipDir = new File(dir2zip);
        //get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[4096];
        int bytesIn = 0;
        //loop through dirList, and zip the files
        for(int i=0; i MAX_SIZE) {
              stopEarlyBecauseTooMuchData = true;
              break;
            }
          }
          //close the Stream
          fis.close();
          zos.closeEntry();

          if (stopEarlyBecauseTooMuchData) {
            Log.warn("GetLogsTask stopEarlyBecauseTooMuchData");
            break;
          }
        }
      }
      catch(Exception e) {
        Log.warn(e);
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy