001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.client;
020
021import org.apache.reef.client.parameters.*;
022import org.apache.reef.runtime.common.client.parameters.ClientPresent;
023import org.apache.reef.tang.formats.ConfigurationModule;
024import org.apache.reef.tang.formats.ConfigurationModuleBuilder;
025import org.apache.reef.tang.formats.OptionalImpl;
026import org.apache.reef.wake.EventHandler;
027import org.apache.reef.wake.remote.RemoteConfiguration;
028
029/**
030 * A ConfigurationModule to fill out for the client configuration.
031 */
032public final class ClientConfiguration extends ConfigurationModuleBuilder {
033
034  /**
035   * Event handler for messages from the running job.
036   * Default implementation just writes message to the log.
037   * A message contains a status and a client-defined message payload.
038   */
039  public static final OptionalImpl<EventHandler<JobMessage>> ON_JOB_MESSAGE = new OptionalImpl<>();
040
041  /**
042   * Handler for the event when a REEF job is submitted to the Resource Manager.
043   * Default implementation just writes the new job ID to the log.
044   */
045  public static final OptionalImpl<EventHandler<SubmittedJob>> ON_JOB_SUBMITTED = new OptionalImpl<>();
046
047  /**
048   * Handler for the event when a submitted REEF Job is running.
049   * Default implementation just writes to the log.
050   */
051  public static final OptionalImpl<EventHandler<RunningJob>> ON_JOB_RUNNING = new OptionalImpl<>();
052
053  /**
054   * Handler for the event when a submitted REEF Job is completed.
055   * Default implementation just writes to the log.
056   */
057  public static final OptionalImpl<EventHandler<CompletedJob>> ON_JOB_COMPLETED = new OptionalImpl<>();
058
059  /**
060   * Handler for the event when a submitted REEF Job has failed.
061   * Default implementation logs an error and rethrows the exception in the client JVM.
062   */
063  public static final OptionalImpl<EventHandler<FailedJob>> ON_JOB_FAILED = new OptionalImpl<>();
064
065  /**
066   * Receives fatal resourcemanager errors. The presence of this error means that the
067   * underlying REEF instance is no longer able to execute REEF jobs. The
068   * actual Jobs may or may not still be running.
069   * Default implementation logs an error and rethrows the exception in the client JVM.
070   */
071  public static final OptionalImpl<EventHandler<FailedRuntime>> ON_RUNTIME_ERROR = new OptionalImpl<>();
072
073  /**
074   * Error handler for events on Wake-spawned threads.
075   * Exceptions that are thrown on wake-spawned threads (e.g. in EventHandlers) will be caught by Wake and delivered to
076   * this handler. Default behavior is to log the exceptions and rethrow them as RuntimeExceptions.
077   */
078  public static final OptionalImpl<EventHandler<Throwable>> ON_WAKE_ERROR = new OptionalImpl<>();
079
080  public static final ConfigurationModule CONF = new ClientConfiguration()
081      .bind(JobMessageHandler.class, ON_JOB_MESSAGE)
082      .bind(JobSubmittedHandler.class, ON_JOB_SUBMITTED)
083      .bind(JobRunningHandler.class, ON_JOB_RUNNING)
084      .bind(JobCompletedHandler.class, ON_JOB_COMPLETED)
085      .bind(JobFailedHandler.class, ON_JOB_FAILED)
086      .bind(ResourceManagerErrorHandler.class, ON_RUNTIME_ERROR)
087      .bindNamedParameter(ClientPresent.class, ClientPresent.YES)
088      .bindNamedParameter(RemoteConfiguration.ErrorHandler.class, ON_WAKE_ERROR)
089      .build();
090}