00001 /* 00002 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 00003 * 00004 * Licensed under the Universal Permissive License v 1.0 as shown at 00005 * http://oss.oracle.com/licenses/upl. 00006 */ 00007 #ifndef COH_PRIORITY_TASK_HPP 00008 #define COH_PRIORITY_TASK_HPP 00009 00010 #include "coherence/lang.ns" 00011 00012 COH_OPEN_NAMESPACE2(coherence,net) 00013 00014 00015 /** 00016 * The PriorityTask interface allows to control the ordering in which a 00017 * service schedules tasks for execution using a thread pool and limit their 00018 * execution times to a specified duration. Instances of PriorityTask 00019 * typically also implement either PriorityTask or Runnable interface. 00020 * 00021 * Depending on the value returned by the #getSchedulingPriority() 00022 * method, the scheduling order will be one of the following: 00023 * <ul> 00024 * <li> #schedule_standard - a task will be scheduled for execution in 00025 * a natural (based on the request arrival time) order; 00026 * <li> #schedule_first - a task will be scheduled in front of any 00027 * equal or lower scheduling priority tasks and executed as soon as any 00028 * of worker threads become available; 00029 * <li> #schedule_immediate - a task will be immediately executed by 00030 * any idle worker thread; if all of them are active, a new thread will 00031 * be created to execute this task. 00032 * </ul> 00033 * 00034 * A best effort will be made to limit the task execution time according to 00035 * the value returned by the #getExecutionTimeoutMillis() method. However, 00036 * it should be noted that: 00037 * <ul> 00038 * <li> for tasks with the scheduling priority of schedule_immediate, factors 00039 * that could make the execution time longer than the timeout value are 00040 * long GC pauses and high network latency; 00041 * <li> if the service has a task backlog (when there are more tasks scheduled 00042 * for execution than the number of available worker threads), the 00043 * request execution time (measured from the client's perspective) for 00044 * tasks with the scheduling priorities of schedule_standard or 00045 * schedule_first could be longer and include the time those tasks were 00046 * kept in a queue before invocation; 00047 * <li> the corresponding service is free to cancel the task execution before 00048 * the task is started and call the #runCanceled method if it's 00049 * known that the client is no longer interested in the results of the 00050 * task execution. 00051 * </ul> 00052 * 00053 * In addition to allowing control of the task execution (as scheduled and 00054 * measured on the server side), the PriorityTask interface could also be used 00055 * to control the request time from the calling thread perspective (measured 00056 * on the client). A best effort will be made to limit the request time (the 00057 * time period that the calling thread is blocked waiting for a response from 00058 * the corresponding service) to the value returned by the 00059 * #getRequestTimeoutMillis() method. 00060 * 00061 * It should be noted that the request timeout value (RT) could be grater 00062 * than, equal to or less than the task execution timeout value (ET). The 00063 * value of RT which is less than ET indicates that even though the task 00064 * execution is allowed to take longer period of time, the client thread will 00065 * not wait for a result of the execution and will be able to handle a timeout 00066 * exception if it arises. Since the time spent by the task waiting in the 00067 * service backlog queue does not count toward the task execution time, a 00068 * value of RT that is equal or slightly greater than ET still leaves a 00069 * possibility that the client thread will throw a TimeoutException before the 00070 * task completes its execution normally on a server. 00071 * 00072 * @author djl 2008.05.15 00073 */ 00074 class COH_EXPORT PriorityTask 00075 : public interface_spec<PriorityTask> 00076 { 00077 // ----- PriorityTask interface ----------------------------------------- 00078 00079 public: 00080 /** 00081 * Obtain this task's scheduling priority. Valid values are one of the 00082 * SCHEDULE_* constants. 00083 * 00084 * @return this task's scheduling priority 00085 */ 00086 virtual int32_t getSchedulingPriority() const = 0; 00087 00088 /** 00089 * Obtain the maximum amount of time this task is allowed to run 00090 * before the corresponding service will attempt to stop it. 00091 * 00092 * The value of #timeout_default indicates a default 00093 * timeout value configured for the corresponding service; the value 00094 * of #timeout_none indicates that this task can execute indefinitely. 00095 * 00096 * If, by the time the specified amount of time passed, the task has 00097 * not finished, the service will attempt to stop the execution by 00098 * using the Thread#interrupt() method. In the case that interrupting 00099 * the thread does not result in the task's termination, the 00100 * #runCanceled method will be called. 00101 * 00102 * @return the execution timeout value in millisecods or one of the 00103 * special TIMEOUT_* values 00104 */ 00105 virtual int64_t getExecutionTimeoutMillis() const = 0; 00106 00107 /** 00108 * Obtain the maximum amount of time a calling thread is willing to 00109 * wait for a result of the request execution. The request time is 00110 * measured on the client side as the time elapsed from the moment a 00111 * request is sent for execution to the corresponding server node(s) 00112 * and includes: 00113 * <ul> 00114 * <li> the time it takes to deliver the request to the executing 00115 * node(s); 00116 * <li> the interval between the time the task is received and placed 00117 * into a service queue until the execution starts; 00118 * <li> the task execution time; 00119 * <li> the time it takes to deliver a result back to the client. 00120 * </ul> 00121 * 00122 * The value of timeout_default indicates a default timeout value 00123 * configured for the corresponding service; the 00124 * value of #timeout_none timeout_none indicates that the client 00125 * thread is willing to wait indefinitely until the task execution 00126 * completes or is canceled by the service due to a task execution 00127 * timeout specified by the #getExecutionTimeoutMillis() value. 00128 * 00129 * If the specified amount of time elapsed and the client has not 00130 * received any response from the server, an 00131 * RequestTimeoutException will be thrown to the caller. 00132 * 00133 * @return the request timeout value in milliseconds or one of the 00134 * special TIMEOUT_* values 00135 */ 00136 virtual int64_t getRequestTimeoutMillis() const = 0; 00137 00138 /** 00139 * This method will be called if and only if all attempts to interrupt 00140 * this task were unsuccesful in stopping the execution or if the 00141 * execution was canceled <b>before</b> it had a chance to run at all. 00142 * 00143 * Since this method is usually called on a service thread, 00144 * implementors must exercise extreme caution since any delay 00145 * introduced by the implementation will cause a delay of the 00146 * corresponding service. 00147 * 00148 * @param fAbandoned true if the task has timed-out, but all 00149 * attempts to interrupt it were unsuccesful in 00150 * stopping the execution; otherwise the task was 00151 * never started 00152 */ 00153 virtual void runCanceled(bool fAbandoned) = 0; 00154 00155 00156 // ----- constants ------------------------------------------------------ 00157 00158 public: 00159 /** 00160 * Scheduling value indicating that this task is to be queued and 00161 * executed in a natural (based on the request arrival time) order. 00162 */ 00163 static const int32_t schedule_standard = 0; 00164 00165 /** 00166 * Scheduling value indicating that this task is to be queued in front 00167 * of any equal or lower scheduling priority tasks and executed as 00168 * soon as any of the worker threads become available. 00169 */ 00170 static const int32_t schedule_first = 1; 00171 00172 /** 00173 * Scheduling value indicating that this task is to be immediately 00174 * executed by any idle worker thread; if all of them are active, a 00175 * new thread will be created to execute this task. 00176 */ 00177 static const int32_t schedule_immediate = 2; 00178 00179 /** 00180 * A special timeout value to indicate that the corresponding 00181 * service's default timeout value should be used. 00182 */ 00183 static const int64_t timeout_default = 0L; 00184 00185 /** 00186 * A special timeout value to indicate that this task or request can 00187 * run indefinitely. 00188 */ 00189 static const int64_t timeout_none = -1L; 00190 }; 00191 00192 COH_CLOSE_NAMESPACE2 00193 00194 #endif // COH_PRIORITY_TASK_HPP