예제 #1
0
  private final void option_req(final GearmanPacket packet, final ServerClient client) {
    /*
     * A client issues this to set an option for the connection in the
     * job server. Returns a OPTION_RES packet on success, or an ERROR
     * packet on failure.
     *
     * Arguments:
     * - Name of the option to set. Possibilities are:
     * 		"exceptions" - Forward WORK_EXCEPTION packets to the client.
     */

    final byte[] option = packet.getArgumentData(0);
    assert option != null;

    final byte[] exceptions = new byte[] {'e', 'x', 'c', 'e', 'p', 't', 'i', 'o', 'n', 's'};

    if (Arrays.equals(option, exceptions)) {
      // exceptions option
      client.setForwardsExceptions(true);

      client.sendPacket(ServerStaticPackets.OPTION_RES_EXCEPTIONS, null /*TODO*/);

    } else {
      // unknown option
      client.sendPacket(ServerStaticPackets.ERROR_UNKNOWN_OPTION, null /*TODO*/);
    }
  }
예제 #2
0
  private final void get_status(final GearmanPacket packet, final ServerClient client) {

    /*
     * A client issues this to get status information for a submitted job.
     *
     * Arguments:
     *  - Job handle that was given in JOB_CREATED packet.
     */

    final byte[] jobHandle = packet.getArgumentData(0);
    assert jobHandle != null;
    final ByteArray jobHandleBA = new ByteArray(jobHandle);

    final ServerJob job = ServerJobAbstract.getJob(jobHandleBA);
    if (job == null) {
      // Send unknown job STATUS_RES packet
      final byte[] unknown = new byte[] {'0'};
      final GearmanPacket status_res =
          new GearmanPacket(
              Magic.RES, Type.STATUS_RES, jobHandle, unknown, unknown, unknown, unknown);
      client.sendPacket(status_res, null /*TODO*/);
      return;
    }

    final GearmanPacket status = job.createStatusResPacket();

    client.sendPacket(status, null /*TODO*/);
  }
예제 #3
0
  private final void text_maxqueue(final String[] args, final ServerClient client) {
    final byte[] funcName = args[1].getBytes(GearmanConstants.UTF_8);
    if (funcName == null) {
      client.sendPacket(ServerStaticPackets.TEXT_INCOMPLETE_ARGS, null /*TODO*/);
      return;
    }
    final ByteArray funcNameBA = new ByteArray(funcName);

    final String sizeStr = args[2];
    if (sizeStr == null) {
      client.sendPacket(ServerStaticPackets.TEXT_INCOMPLETE_ARGS, null /*TODO*/);
      return;
    }

    final int size;
    try {
      size = Integer.parseInt(sizeStr);
    } catch (NumberFormatException e) {
      client.sendPacket(ServerStaticPackets.TEXT_OK, null /*TODO*/);
      return;
    }

    /*
     * FWIX CHANGE
     */
    // final ServerFunction func = this.funcSet.getFunctionIfDefined(funcNameBA);

    funcSet.setMaxQueueByName(funcNameBA, size);
  }
예제 #4
0
  private final void worker_time_status(final String[] args, final ServerClient client) {
    synchronized (this.server.getClientSet()) {
      for (ServerClient c : this.server.getClientSet())
        client.sendPacket(c.getTimeStatus(), null /*TODO*/);
    }

    client.sendPacket(ServerStaticPackets.TEXT_DONE, null /*TODO*/);
  }
예제 #5
0
  private final void work_status(final GearmanPacket packet, final ServerClient client) {
    /*
     * This is sent to update the server (and any listening clients)
     * of the status of a running job. The client should send these
     * periodically for long running jobs to update the percentage
     * complete. The job server should store this information so a client
     * who issued a background command may retrieve it later with a
     * GET_STATUS request.
     *
     * Arguments:
     * - NULL byte terminated job handle.
     * - NULL byte terminated percent complete numerator.
     * - Percent complete denominator.
     */

    final byte[] jobHandle = packet.getArgumentData(0);
    assert jobHandle != null;
    final ByteArray jobHandleBA = new ByteArray(jobHandle);

    final byte[] num = packet.getArgumentData(1);
    assert num != null;

    final byte[] den = packet.getArgumentData(2);
    assert den != null;

    final ServerJob job = ServerJobAbstract.getJob(jobHandleBA);
    if (job == null) {
      client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/);
    } else {
      packet.setMagic(Magic.RES);
      job.setStatus(num, den);
      job.sendPacket(packet);
    }
  }
예제 #6
0
  private final void work_data(final GearmanPacket packet, final ServerClient client) {
    /*
     * This is sent to update the client with data from a running job. A
     * client should use this when it needs to send updates, send partial
     * results, or flush data during long running jobs. It can also be
     * used to break up a result so the client does not need to buffer
     * the entire result before sending in a WORK_COMPLETE packet.
     *
     * Arguments:
     * - NULL byte terminated job handle.
     * - Opaque data that is returned to the client.
     */

    final byte[] jobHandle = packet.getArgumentData(0);
    assert jobHandle != null;
    final ByteArray jobHandleBA = new ByteArray(jobHandle);

    final ServerJob job = ServerJobAbstract.getJob(jobHandleBA);
    if (job == null) {
      client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/);
    } else {
      packet.setMagic(Magic.RES);
      job.sendPacket(packet);
    }
  }
예제 #7
0
  /**
   * Called when a text-based packet is received. When a packet with type -1 is received, it's
   * assumed to be a text-based packet. The first argument (The command argument in a text-based
   * packet) is checked against the set of known commands. Then, the command is executed or an
   * exception is returned to the client if the command is unknown.
   *
   * @param packet The received packet with a type of -1
   * @param client The client who received the packet
   */
  private final void text_packet(final GearmanPacket packet, final ServerClient client) {
    final String pkt = new String(packet.toBytes(), GearmanConstants.UTF_8);
    final String[] args = pkt.trim().split("[ \t]");

    if (args[0].equalsIgnoreCase("workers")) {
      text_workers(args, client);
      return;
    } else if (args[0].equalsIgnoreCase("status")) {
      text_status(args, client);
      return;
    } else if (args[0].equalsIgnoreCase("maxqueue")) {
      text_maxqueue(args, client);
      return;
    } else if (args[0].equalsIgnoreCase("shutdown")) {
      text_shutdown(args, client);
      return;
    } else if (args[0].equalsIgnoreCase("version")) {
      text_version(args, client);
      return;
    } else if (args[0].equalsIgnoreCase("throttle")) {
      update_throttle(args, client);
      return;
    } else if (args[0].equalsIgnoreCase("memstats")) {
      getMemStats(client);
      return;
    } else if (args[0].equalsIgnoreCase("workertime")) {
      worker_time_status(args, client);
      return;
    } else {
      client.sendPacket(ServerStaticPackets.TEXT_UNKNOWN_COMMAND, null /*TODO*/);
    }
  }
예제 #8
0
  private final void echo_req(final GearmanPacket packet, final ServerClient client) {
    /*
     * When a job server receives this request, it simply generates a
     * ECHO_RES packet with the data. This is primarily used for testing
     * or debugging.
     *
     * Arguments:
     * - Opaque data that is echoed back in response.
     */

    packet.setMagic(Magic.RES);
    client.sendPacket(packet, null /*TODO*/);
  }
예제 #9
0
  public void getMemStats(final ServerClient client) {

    StringBuilder sb = new StringBuilder();
    sb.append(Main.memUsed());
    sb.append('\t');
    sb.append(Main.getThreadCount());
    sb.append('\t');
    sb.append(Main.getActiveThreadCount());
    sb.append('\t');
    sb.append(Main.getUpTime());
    sb.append('\n');
    client.sendPacket(GearmanPacket.createTEXT(sb.toString()), null /*TODO*/);
    return;
  }
예제 #10
0
  private final void work_fail(final GearmanPacket packet, final ServerClient client) {
    /*
     * This is to notify the server (and any listening clients) that
     * the job failed.
     *
     * Arguments:
     * - Job handle.
     */

    final byte[] jobHandle = packet.getArgumentData(0);
    assert jobHandle != null;
    final ByteArray jobHandleBA = new ByteArray(jobHandle);

    final ServerJob job = ServerJobAbstract.getJob(jobHandleBA);
    if (job == null) {
      client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/);
    } else {
      job.workComplete(packet);
    }
  }
예제 #11
0
  private final void work_exception(final GearmanPacket packet, final ServerClient client) {
    /*
     * This is to notify the server (and any listening clients) that
     * the job failed with the given exception.
     *
     * Arguments:
     * - NULL byte terminated job handle.
     * - Opaque data that is returned to the client as an exception.
     */

    // Note: The protocol states this packet notifies the server that the specified job
    // has failed. However, the C server does not fail the Job.  It is effectively a
    // WORK_WARNING packet that is only sent to clients that have specified they want
    // exceptions forwarded to them.  This server will do the same as long as the C
    // server does so.

    /*
     * This is sent to update the client with data from a running job. A
     * client should use this when it needs to send updates, send partial
     * results, or flush data during long running jobs. It can also be
     * used to break up a result so the client does not need to buffer
     * the entire result before sending in a WORK_COMPLETE packet.
     *
     * Arguments:
     * - NULL byte terminated job handle.
     * - Opaque data that is returned to the client.
     */

    final byte[] jobHandle = packet.getArgumentData(0);
    ;
    assert jobHandle != null;
    final ByteArray jobHandleBA = new ByteArray(jobHandle);

    final ServerJob job = ServerJobAbstract.getJob(jobHandleBA);
    if (job == null) {
      client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/);
    } else {
      packet.setMagic(Magic.RES);
      job.sendPacket(packet);
    }
  }
예제 #12
0
  public void update_throttle(
      final String[] args, final ServerClient client) { // String function, Integer throttle){
    String function = args[1];
    Integer throttle = 0;
    StringBuilder sb = new StringBuilder();
    if (args[2].equalsIgnoreCase("NOTHROTTLE")) {
      System.out.println("REMOVE THROTTLE" + function);
      Main.removeThrottle(function);
      sb.append(function);
      sb.append('\t');
      sb.append("remove throttle");
      sb.append('\n');
    } else if (args[2].equalsIgnoreCase("KILL")) {

      System.out.println("KILL FUNCTION");
      RemoveClass remove = new RemoveClass(function);
      Thread t = new Thread(remove);
      t.start();

      sb.append(function);
      sb.append('\t');
      sb.append("kill");
      sb.append('\n');

    } else {

      throttle = Integer.parseInt(args[2]);
      ;
      if (throttle != null && throttle >= 0) {
        System.out.println("Putthing throttle: " + throttle + " for function " + function);
        Main.setThrottle(function, throttle);
        sb.append(function);
        sb.append('\t');
        sb.append(throttle);
        sb.append('\n');
      }
    }

    client.sendPacket(GearmanPacket.createTEXT(sb.toString()), null /*TODO*/);
    return;
  }
예제 #13
0
  private final void work_complete(final GearmanPacket packet, final ServerClient client) {
    /*
     * This is to notify the server (and any listening clients) that
     * the job completed successfully.
     *
     * Arguments:
     * - NULL byte terminated job handle.
     * - Opaque data that is returned to the client as a response.
     */

    final byte[] jobHandle = packet.getArgumentData(0);
    assert jobHandle != null;
    final ByteArray jobHandleBA = new ByteArray(jobHandle);

    final ServerJob job = ServerJobAbstract.getJob(jobHandleBA);
    if (job == null) {
      client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/);
    } else {

      // Construct a WORK_COMPLETE response packet
      client.jobCOmplete();
      job.workComplete(packet);
    }
  }
예제 #14
0
  /**
   * Once a packet has been acquired from a client, it's processed here. The packet is processed in
   * compliance with the gearman protocol.
   *
   * @param packet The packet sent by the client
   * @param client The client associated with the client that sent the packet
   */
  public final void execute(final GearmanPacket packet, final ServerClient client) {

    switch (packet.getPacketType()) {

        // Text Packets
      case TEXT:
        text_packet(packet, client);
        return;

        // Binary Packets
      case CAN_DO:
        can_do(packet, client);
        return;
      case CAN_DO_TIMEOUT:
        can_do(packet, client);
        return;
      case CANT_DO:
        cant_do(packet, client);
        return;
      case ECHO_REQ:
        echo_req(packet, client);
        return;
      case GET_STATUS:
        get_status(packet, client);
        return;
      case GRAB_JOB:
        grab_job(packet, client);
        return;
      case GRAB_JOB_UNIQ:
        grab_job_uniq(packet, client);
        return;
      case OPTION_REQ:
        option_req(packet, client);
        return;
      case PRE_SLEEP:
        pre_sleep(packet, client);
        return;
      case RESET_ABILITIES:
        System.out.println("New worker has joined");
        reset_abilities(packet, client);
        return;
      case SET_CLIENT_ID:
        set_client_id(packet, client);
        return;
      case SUBMIT_JOB:
        submit_job(packet, client, ServerJob.JobPriority.MID, false);
        return;
      case SUBMIT_JOB_BG:
        submit_job(packet, client, ServerJob.JobPriority.MID, true);
        return;
      case SUBMIT_JOB_HIGH:
        submit_job(packet, client, ServerJob.JobPriority.HIGH, false);
        return;
      case SUBMIT_JOB_HIGH_BG:
        submit_job(packet, client, ServerJob.JobPriority.HIGH, true);
        return;
      case SUBMIT_JOB_LOW:
        submit_job(packet, client, ServerJob.JobPriority.LOW, false);
        return;
      case SUBMIT_JOB_LOW_BG:
        submit_job(packet, client, ServerJob.JobPriority.LOW, true);
        return;
      case WORK_COMPLETE:
        work_complete(packet, client);
        return;
      case WORK_DATA:
      case WORK_WARNING:
        work_data(packet, client);
        return;
      case WORK_EXCEPTION:
        work_exception(packet, client);
        return;
      case WORK_FAIL:
        work_fail(packet, client);
        return;
      case WORK_STATUS:
        work_status(packet, client);
        return;

        // Response Only Packets
      case NOOP:
      case JOB_CREATED:
      case NO_JOB:
      case ECHO_RES:
      case ERROR:
      case STATUS_RES:
      case OPTION_RES:
      case JOB_ASSIGN:
      case JOB_ASSIGN_UNIQ:

        // Packets Not Yet Implemented
      case ALL_YOURS:
      case SUBMIT_JOB_EPOCH:
      case SUBMIT_JOB_SCHED:
        client.sendPacket(ServerStaticPackets.ERROR_BAD_COMMAND, null /*TODO*/);
        return;

        // Unknown Command
      default:
        client.sendPacket(ServerStaticPackets.ERROR_BAD_COMMAND, null /*TODO*/);
        return;
    }
  }
예제 #15
0
 private final void text_version(final String[] args, final ServerClient client) {
   client.sendPacket(ServerStaticPackets.TEXT_VERSION, null /*TODO*/);
 }