1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.persistence.model;
18
19
20
21
22
23 public class AllocationLive implements Runnable {
24
25 private final AllocationInfo alloc;
26 private long idleDeathTime = -1;
27 private long oldDeathTime;
28 private HeadResourceInfo headResource;
29
30
31 private Object lifeControllerLock = new Object();
32 private JobExecutionInfo currentJobExecution = null;
33 private boolean registered = false;
34
35 public AllocationLive(AllocationInfo alloc, DaemonConfigDesc config) {
36 this.alloc = alloc;
37 this.headResource = config.getHeadResource();
38 this.oldDeathTime = alloc.getWhenAttained().getTime() + headResource.getMaxHijackMillis();
39 }
40
41 public boolean isRegistered() {
42 return registered;
43 }
44
45 public void registerAsRunningOn(ComputeResourceInfo compRes) {
46 registered = true;
47 alloc.setCompResource(compRes);
48 }
49
50 public void startIdleCountdown() {
51 setIdleDeathTime(System.currentTimeMillis() + headResource.getMaxIdleMillis());
52 }
53
54 public void cancelIdleCountdown() {
55 setIdleDeathTime(-1);
56 }
57
58 private void setIdleDeathTime(long time) {
59 synchronized (lifeControllerLock) {
60 idleDeathTime = time;
61 lifeControllerLock.notifyAll();
62 }
63 }
64
65 public JobExecutionInfo getProcessingExecution() {
66 synchronized (lifeControllerLock) {
67 return currentJobExecution;
68 }
69 }
70
71 public JobExecutionInfo extractProcessingExecution() {
72 synchronized (lifeControllerLock) {
73 JobExecutionInfo result = currentJobExecution;
74 currentJobExecution = null;
75 return result;
76 }
77 }
78
79 public JobExecutionInfo getNextProcessingExecutionBlocking() {
80 synchronized (lifeControllerLock) {
81 while (currentJobExecution == null && alloc.getWhenReleased() == null)
82 try { lifeControllerLock.wait(); } catch (InterruptedException e) {}
83
84 return currentJobExecution;
85 }
86 }
87
88 public boolean setProcessingExecution(JobExecutionInfo execution) {
89 synchronized (lifeControllerLock) {
90 if (alloc.getWhenReleased() != null) return false;
91
92 currentJobExecution = execution;
93 execution.setAllocation(alloc);
94 cancelIdleCountdown();
95 lifeControllerLock.notifyAll();
96 return true;
97 }
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111 public void run() {
112
113 boolean hijack = headResource.getMaxHijackMillis() > 0;
114 if (!hijack && headResource.getMaxIdleMillis() <= 0) return;
115
116
117 boolean isIdleCutTime;
118 synchronized (lifeControllerLock) {
119
120 while (true) {
121 isIdleCutTime = (0 <= idleDeathTime && idleDeathTime <= oldDeathTime);
122 long remaining = (isIdleCutTime ? idleDeathTime : oldDeathTime) - System.currentTimeMillis();
123 if (remaining <= 0) break;
124 try {
125 lifeControllerLock.wait(remaining);
126 } catch (InterruptedException e) {
127
128
129 }
130 if (alloc.getWhenReleased() != null || alloc.getWhenDisposed() != null) break;
131 }
132
133
134
135
136
137
138
139
140
141
142 }
143 }
144 }