1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.request
;
15 import java
.util
.concurrent
.CountDownLatch
;
17 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfData
;
21 * <b><u>TmfDataRequest</u></b>
23 * TmfDataRequests are used to obtain blocks of contiguous data from a data
24 * provider. Open ranges can be used, especially for continuous streaming.
26 * The request is processed asynchronously by a TmfProvider and, as blocks
27 * of data become available, handleData() is invoked synchronously for each
28 * block. Upon return, the data instances go out of scope and become eligible
29 * for gc. It is is thus the responsibility of the requester to either clone
30 * or keep a reference to the data it wishes to track specifically.
32 * This data block approach is used to avoid busting the heap for very
33 * large trace files. The block size is configurable.
35 * The TmfProvider indicates that the request is completed by calling done().
36 * The request can be canceled at any time with cancel().
39 *<pre><code><i>TmfTimeWindow range = new TmfTimewindow(...);
40 *TmfDataRequest<DataType[]> request = new TmfDataRequest<DataType[]>(DataType.class, 0, NB_EVENTS, BLOCK_SIZE) {
41 * public void handleData() {
42 * DataType[] data = request.getData();
43 * for (DataType e : data) {
47 * public void handleSuccess() {
51 * public void handleFailure() {
55 * public void handleCancel() {
60 *fProcessor.process(request, true);
63 * TODO: Consider decoupling from "time range", "rank", etc and for the more
64 * generic notion of "criteria". This would allow to extend for "time range", etc
65 * instead of providing specialized constructors. This also means removing the
66 * criteria info from the data structure (with the possible exception of fNbRequested).
67 * The nice thing about it is that it would prepare us well for the coming generation
70 * TODO: Implement request failures (codes, etc...)
72 public abstract class TmfDataRequest
<T
extends TmfData
> implements ITmfDataRequest
<T
> {
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
78 // The default maximum number of events per chunk
79 public static final int DEFAULT_BLOCK_SIZE
= 1000;
81 // The request count for all the events
82 public static final int ALL_DATA
= Integer
.MAX_VALUE
;
84 private static int fRequestNumber
= 0;
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
90 private final Class
<T
> fDataType
;
91 private final ExecutionType fExecType
;
92 private final int fRequestId
; // A unique request ID
93 private final int fIndex
; // The index (rank) of the requested event
94 private final int fNbRequested
; // The number of requested events (ALL_DATA for all)
95 private int fNbRead
; // The number of reads so far
97 private CountDownLatch startedLatch
= new CountDownLatch(1);
98 private CountDownLatch completedLatch
= new CountDownLatch(1);
99 private boolean fRequestRunning
= false;
100 private boolean fRequestCompleted
= false;
101 private boolean fRequestFailed
= false;
102 private boolean fRequestCanceled
= false;
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * Resets the request counter (used for testing)
111 public static void reset() {
116 * Default constructor
118 * @param dataType the requested data type
120 public TmfDataRequest(Class
<T
> dataType
) {
121 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
124 public TmfDataRequest(Class
<T
> dataType
, ExecutionType execType
) {
125 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, execType
);
129 * @param dataType the requested data type
130 * @param nbRequested the number of data items requested
132 public TmfDataRequest(Class
<T
> dataType
, int index
) {
133 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
136 public TmfDataRequest(Class
<T
> dataType
, int index
, ExecutionType execType
) {
137 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, execType
);
141 * @param dataType the requested data type
142 * @param index the index (rank) of the first event requested
143 * @param blockSize the number of data items per block
145 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
) {
146 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
149 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, ExecutionType execType
) {
150 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, execType
);
154 * @param dataType the requested data type
155 * @param index the index (rank) of the first event requested
156 * @param nbRequested the number of data items requested
157 * @param blockSize the number of data items per block
159 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
) {
160 this(dataType
, index
, nbRequested
, blockSize
, ExecutionType
.FOREGROUND
);
163 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
, ExecutionType execType
) {
164 fRequestId
= fRequestNumber
++;
165 fDataType
= dataType
;
167 fNbRequested
= nbRequested
;
168 fExecType
= execType
;
170 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "created");
176 @SuppressWarnings("unused")
177 private TmfDataRequest(TmfDataRequest
<T
> other
) {
178 this(null, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
);
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 * @return the request ID
189 public int getRequestId() {
194 * @return the index of the first event requested
197 public int getIndex() {
202 * @return the index of the first event requested
205 public ExecutionType
getExecType() {
210 * @return the number of requested events (ALL_DATA = all)
213 public int getNbRequested() {
218 * @return the number of events read so far
221 public synchronized int getNbRead() {
226 * @return indicates if the request is completed
229 public synchronized boolean isRunning() {
230 return fRequestRunning
;
234 * @return indicates if the request is completed
237 public synchronized boolean isCompleted() {
238 return fRequestCompleted
;
242 * @return indicates if the request is canceled
245 public synchronized boolean isFailed() {
246 return fRequestFailed
;
250 * @return indicates if the request is canceled
253 public synchronized boolean isCancelled() {
254 return fRequestCanceled
;
258 * @return the requested data type
261 public Class
<T
> getDataType() {
265 // ------------------------------------------------------------------------
267 // ------------------------------------------------------------------------
270 * Sets the data object to specified value. To be called by the
271 * asynchronous method implementor.
273 * @param data Data value to set.
276 * Handle a block of incoming data. This method is called every time
277 * a block of data becomes available.
279 * - Data items are received in the order they appear in the stream.
280 * - Called by the request processor, in its execution thread, every time a
281 * block of data becomes available.
282 * - Request processor performs a synchronous call to handlePartialResult()
283 * i.e. its execution threads holds until handlePartialData() returns.
284 * - Original data items are disposed of on return i.e. keep a reference
285 * (or a copy) if some persistence is needed between invocations.
286 * - When there is no more data, done() is called.
288 * @param events - an events
291 public void handleData(T data
) {
298 public void handleStarted() {
302 * Handle the completion of the request. It is called when there is no more
303 * data available either because:
304 * - the request completed normally
305 * - the request failed
306 * - the request was canceled
308 * As a convenience, handleXXXX methods are provided. They are meant to be
309 * overridden by the application if it needs to handle these conditions.
312 public void handleCompleted() {
313 if (fRequestFailed
) {
314 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "failed");
317 else if (fRequestCanceled
) {
318 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "cancelled");
322 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "succeeded");
328 public void handleSuccess() {
332 public void handleFailure() {
336 public void handleCancel() {
340 * To suspend the client thread until the request starts (or is
343 * @throws InterruptedException
345 public void waitForStart() throws InterruptedException
{
346 while (!fRequestRunning
) {
347 startedLatch
.await();
352 * To suspend the client thread until the request completes (or is
355 * @throws InterruptedException
358 public void waitForCompletion() throws InterruptedException
{
359 while (!fRequestCompleted
) {
360 completedLatch
.await();
365 * Called by the request processor upon starting to service the request.
368 public void start() {
369 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "starting");
371 fRequestRunning
= true;
374 startedLatch
.countDown();
375 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "started");
379 * Called by the request processor upon completion.
383 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "completing");
385 if (!fRequestCompleted
) {
386 fRequestRunning
= false;
387 fRequestCompleted
= true;
391 completedLatch
.countDown();
395 * Called by the request processor upon failure.
400 fRequestFailed
= true;
406 * Called by the request processor upon cancellation.
409 public void cancel() {
411 fRequestCanceled
= true;
416 // ------------------------------------------------------------------------
418 // ------------------------------------------------------------------------
421 // All requests have a unique id
422 public int hashCode() {
423 return getRequestId();
427 public boolean equals(Object other
) {
428 if (other
instanceof TmfDataRequest
<?
>) {
429 TmfDataRequest
<?
> request
= (TmfDataRequest
<?
>) other
;
430 return (request
.fDataType
== fDataType
) &&
431 (request
.fIndex
== fIndex
) &&
432 (request
.fNbRequested
== fNbRequested
);
438 public String
toString() {
439 return "[TmfDataRequest(" + fRequestId
+ "," + fDataType
.getSimpleName() +
440 "," + fIndex
+ "," + fNbRequested
+ ")]";