summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/demos/workpool-distributed/src/main/java/workpool/WorkpoolServiceImpl.java
blob: 52b88af42cb53f94b1aaad4c0ea8a604b98e2001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package workpool;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import org.apache.tuscany.sca.core.context.CallableReferenceImpl;
import org.apache.tuscany.sca.databinding.annotation.DataBinding;
import org.osoa.sca.ComponentContext;
import org.osoa.sca.annotations.Context;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;
import org.apache.tuscany.sca.databinding.job.Job;
import org.apache.tuscany.sca.databinding.job.JobDataMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * An implementation of the Workpool service.
 */
@Service(WorkpoolService.class)
@Scope("COMPOSITE")
@DataBinding("org.apache.tuscany.sca.databinding.job.Job")
public class WorkpoolServiceImpl implements WorkpoolService,
        WorkerServiceCallback {

    /* incoming job queue */
    private LinkedBlockingQueue<Job> queue = new LinkedBlockingQueue<Job>(5000);
    private CallableReferenceImpl<Trigger> trigger = null;
    private Trigger forwardResult = null;
    /* counter for job's number fetched from the queue and sent to the Worker */
    private AtomicInteger jobSent = new AtomicInteger(0);
    /* time for initHandleResult */
    private AtomicLong initHandleResult = new AtomicLong(0);
    /* time for endHandleResult */
    private AtomicLong endHandleResult = new AtomicLong(0);
    /*
     * number of job computed, this will be exposed in order to be used to
     * firing rules
     */
    private long jobComputed = 0;
    /* same as above */
    private AtomicLong elapsedTime = new AtomicLong(0);
    /* this is for comuputing averageServiceTime */
    private long times = 1;
    /* this is for computing averageArrivalTime */
    private long timesArrival = 1;
    private ReentrantLock arrivalLock = new ReentrantLock();
    private long arrivalPrevious = -1;
    // private AtomicBoolean processingStopped = new AtomicBoolean(false);
    private boolean processingStopped = false;
    // private LinkedBlockingQueue<Trigger> triggers = new
    // LinkedBlockingQueue<Trigger>();
    @Context
    protected ComponentContext workpoolContext;
    private CallableReferenceImpl<WorkpoolManager> manager;
    private long previousSubmitTime = -1;
    private boolean firstTime = true;
    private boolean first = true;
    private long start = 0;
    private long end = 0;
    private double averageServiceTime = 0;
    private double averageArrivalTime = 0;
    private int workersNo = 0;
    private final Job nullJob = new NullJob();
    /* This is useful for counting the start and end */
    private Logger log = Logger.getLogger(WorkpoolServiceImpl.class.getName());
    private ReentrantLock handleResultLock = new ReentrantLock();
    private ReentrantLock postWorkerReferenceLock = new ReentrantLock();
    private ConcurrentHashMap<String, WorkerService> cacheReference = new ConcurrentHashMap<String, WorkerService>();
    private CallableReferenceImpl<WorkpoolService> myReference;
    private String previuosURI = "";
    private long time = 0;

    private void computeAverageTime() {
        long actualServiceTime = 0;
        // if the processing is finished
        if (processingStopped)
            return;

        if (firstTime == true) {
            this.previousSubmitTime = System.currentTimeMillis();
            this.averageServiceTime = 0;
            firstTime = false;
        } else {
            actualServiceTime = System.currentTimeMillis()
                    - this.previousSubmitTime;
            this.previousSubmitTime = System.currentTimeMillis();
            averageServiceTime = ((averageServiceTime * times) + actualServiceTime)
                    / (times + 1);
            ++times;
        }
    }

    public void submit(Job j) {
        try {
            // log.info("Submit job in queue -->"+ j.getType());
            // processingStopped.set(false);
            try {
                arrivalLock.lock();
                if (this.arrivalPrevious == -1) {
                    arrivalPrevious = System.currentTimeMillis();
                    averageArrivalTime = 0;
                }
                double actualArrivalTime = System.currentTimeMillis()
                        - arrivalPrevious;
                averageArrivalTime = ((averageArrivalTime * timesArrival) + actualArrivalTime)
                        / (timesArrival + 1);
                arrivalPrevious = System.currentTimeMillis();
                ++timesArrival;
            } finally {
                arrivalLock.unlock();
            }
            queue.put(j);
        } catch (Exception e) {
            log.info("Exception in queue");
            queue.clear();
            e.printStackTrace();
        }
    }

    public double getArrivalTime() {
        return this.averageArrivalTime;
    }

    public double getServiceTime() {
        return this.averageServiceTime;
    }

    public void receiveResult(Job resultType, boolean reuse, String workerURI) {

        if (reuse) {
            queue.add(resultType);
            return;
        }

        computeAverageTime();
        Job job = null;
        try {
            job = queue.take();
        } catch (InterruptedException e) {
            // TODO Better exception handling --> see Exception antipattern doc
            e.printStackTrace();
            return;
        }

        if ((job != null) && (job.eos() == false)) {
            int nameIndex = workerURI.indexOf("/");
            String workerName = workerURI.substring(0, nameIndex - 1);
            log.info("Sending job to worker --> " + workerName);
            WorkerService worker = workpoolContext.getService(
                    WorkerService.class, workerName);
            worker.compute(job);
        }

        JobDataMap map = ((ResultJob) resultType).getDataMap();
        if (map != null) {
            ++jobComputed;
            Object obj = map.getJobDataObject("result");
            System.out.println("Result = " + ((Double) obj).doubleValue());
        }

    }

    public void start() {
        log.info("WorkpoolServiceComponent started...");
        myReference = (CallableReferenceImpl) workpoolContext
                .createSelfReference(WorkpoolService.class, "WorkpoolService");
        myReference.getService();
    }

    /*
     * 
     * This method is called by WorkpoolManagerImpl, when it creates a new
     * worker component in order to dispatch worker to the WorkpoolServiceImpl
     * @param CallableReferenceImpl reference - a dynamically created reference
     * from the Worker
     */
    public void PostWorkerReference(
            CallableReferenceImpl<WorkerService> reference) {

        try {
            long initPostWorkerReference;
            long endPostWorkerReference;
            this.postWorkerReferenceLock.lock();

            initPostWorkerReference = System.currentTimeMillis();
            WorkerService worker;
            worker = reference.getService();
            worker.start();

            ++workersNo;
            if (myReference != null) {

                // Job poison = new ResultJob();
                this.postWorkerReferenceLock.unlock();
                log.info("Sending null job to worker");
                worker.computeFirstTime(nullJob, myReference);
                // queue.put(poison);
                endPostWorkerReference = System.currentTimeMillis();
                System.out.println("Time PostWorker ="
                        + (endPostWorkerReference - initPostWorkerReference));
            } else {
                log.info("myReference is null");

            }
        } catch (Exception e) {
            postWorkerReferenceLock.unlock();
        } finally {
        }

    }

    /*
     * FIXME This method currently is not used because i've not yet ready
     * dynamic wire injection
     */

    public void PostWorkerName(String referenceName) {
        /* TODO Do something similar to PostWorkerReference */
    }

    private void printComputingTime(Job j) {

        if (first == true) {
            first = false;
            start = System.currentTimeMillis();
            end = System.currentTimeMillis();
        } else {
            end = System.currentTimeMillis();
            System.out.println("Elapsed Time = " + (end - start));
            elapsedTime.set(end - start);
        }
        /*
         * i could use reflection or instance of (but it's a penalty kick) , or
         * an object as result, but i'd prefer a job so i've defined a
         * RESULT_JOB There're in the system three kind of jobs: RESULT_JOB,
         * NULL_JOB, DEFAULT_JOB
         */
        if ((j != null) && (j.getType() == Job.RESULT_JOB)) {
            jobComputed++;
            ResultJob result = (ResultJob) j;
            JobDataMap map = result.getDataMap();
            if (map != null) {
                Double doubleValue = (Double) map.getJobDataObject("result");
                System.out
                        .println("ResultValue = " + doubleValue.doubleValue());
            }

        }

    }

    public void handleResult(Job resultType, boolean reuse, String workerURI,
            CallableReferenceImpl<WorkerService> worker, boolean newWorker) {
        initHandleResult.set(System.nanoTime());
        if (reuse) {
            log.info("Reusing a job..");
            queue.add(resultType);
            return;
        }
        // init job variable
        Job job;
        if (newWorker)
            System.out.println("newWorkerActivation= " + System.nanoTime());
        printComputingTime(resultType);

        try {
            job = queue.take();
        } catch (Exception e) {
            log.info("Exception during fetching the queue");
            e.printStackTrace();
            return;
        }

        try {
            // it needs to be locked because multiple threads could invoke this.
            handleResultLock.lock();
            if (previuosURI.equals("")) {
                time = System.currentTimeMillis();
                this.previuosURI = workerURI;
            } else {
                if (previuosURI.equals(workerURI))
                    System.out.println("Complete ComputeTime for an item ="
                            + (time - System.currentTimeMillis()));
            }
            if (job.eos()) {
                long endTime = System.currentTimeMillis();
                /* checking for EOS */
                if (processingStopped == false) {
                    processingStopped = true;
                    System.out.println("GOT EOS in time=" + (endTime - start));
                    log.info("Stop autonomic cycle..");
                    /*
                     * I'm doing this because i want that in the termination i
                     * would have more jobs with eos == true than workers. So
                     * i'm sure that every worker removes itself from its
                     * manager. I do it only one time. This is necessary because
                     * i have a variable number of workers. The number of
                     * workers in the system might change every time the rule
                     * engine cycle gets executed.
                     */
                    ResultJob poison = new ResultJob();
                    for (int i = 0; i < workersNo; ++i) {
                        try {

                            queue.put(poison);

                        } catch (Exception e) {
                            log.info("Cannot duplicate poison tokens");
                            break;
                        }

                    }
                    manager.getService().stopAutonomicCycle();
                }
            }
            computeAverageTime();
            System.out.println("AverageTime =" + averageServiceTime);
            if (job != null) {

                WorkerService workerService;
                /*
                 * the workpool has a high reuse, i always call the same
                 * component set or un superset or subset, so i cache it. When
                 * the WorkpoolManager will remove an item, it removes still
                 * this cache entry
                 */
                if (!cacheReference.containsKey(workerURI)) {
                    workerService = worker.getService();
                    handleResultLock.unlock();
                    cacheReference.put(workerURI, workerService);
                } else {
                    handleResultLock.unlock();
                    workerService = cacheReference.get(workerURI);
                }
                // it's still a penalty kick locking compute because it's going
                // to be scheduled whereas it's async.
                workerService.compute(job);
                log.info("Sent job #" + jobSent.incrementAndGet()
                        + " Queue size " + queue.size());
                endHandleResult.set(System.nanoTime());
                System.out
                        .println("begin:handleResult ==> end:handleResult:compute = "
                                + (endHandleResult.addAndGet(-(initHandleResult
                                        .get())) / 1000000));
            }
        } catch (Exception e) {
            handleResultLock.unlock();
        }
    }

    public void evictAll() {
        cacheReference.clear();
    }

    public void evict(String workerURI) {
        if (cacheReference.containsKey(workerURI)) {
            cacheReference.remove(workerURI);
        }

    }

    public int estimatedQueueSize() {
        return queue.size();
    }

    public long getElapsedTime() {
        return elapsedTime.get();
    }

    public long getJobComputed() {
        return jobComputed;
    }

    public void registerManager(
            CallableReferenceImpl<WorkpoolManager> createSelfReference) {
        manager = createSelfReference;

    }

    public void stop() {
        // TODO Auto-generated method stub

    }

    public void addTrigger(CallableReferenceImpl<Trigger> reference) {
        this.trigger = reference;
        this.forwardResult = reference.getService();

    }

    public void removeTrigger() {
        this.trigger = null;
        this.forwardResult = null;
    }
}