-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TimeBasedTaskScheduler.java
172 lines (156 loc) · 6.03 KB
/
TimeBasedTaskScheduler.java
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
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.scheduler;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import org.neo4j.scheduler.JobScheduler.Group;
import org.neo4j.scheduler.JobScheduler.JobHandle;
import org.neo4j.time.SystemNanoClock;
final class TimeBasedTaskScheduler implements Runnable
{
private static final ScheduledJobHandle END_SENTINEL = new ScheduledJobHandle( null, null, 0, 0 );
private static final long NO_TASKS_PARK = TimeUnit.MINUTES.toNanos( 10 );
private static final Comparator<ScheduledJobHandle> DEADLINE_COMPARATOR =
Comparator.comparingLong( handle -> handle.nextDeadlineNanos );
private final SystemNanoClock clock;
private final ThreadPoolManager pools;
private final AtomicReference<ScheduledJobHandle> inbox;
private volatile Thread timeKeeper;
private volatile boolean stopped;
// This field is only access by the time keeper thread:
private final PriorityQueue<ScheduledJobHandle> delayedTasks;
TimeBasedTaskScheduler( SystemNanoClock clock, ThreadPoolManager pools )
{
this.clock = clock;
this.pools = pools;
inbox = new AtomicReference<>( END_SENTINEL );
delayedTasks = new PriorityQueue<>( DEADLINE_COMPARATOR );
}
public JobHandle submit( Group group, Runnable job, long initialDelayNanos, long reschedulingDelayNanos )
{
long now = clock.nanos();
long nextDeadlineNanos = now + initialDelayNanos;
ScheduledJobHandle task = new ScheduledJobHandle( group, job, nextDeadlineNanos, reschedulingDelayNanos );
task.next = inbox.getAndSet( task );
LockSupport.unpark( timeKeeper );
return task;
}
public long tick()
{
long now = clock.nanos();
sortInbox();
long timeToNextDeadlineSinceStart = scheduleDueTasks( now );
long processingTime = clock.nanos() - now;
return timeToNextDeadlineSinceStart - processingTime;
}
private void sortInbox()
{
ScheduledJobHandle newTasks = inbox.getAndSet( END_SENTINEL );
while ( newTasks != END_SENTINEL )
{
// Capture next chain link before enqueueing among delayed tasks.
ScheduledJobHandle next;
do
{
next = newTasks.next;
}
while ( next == null );
enqueueTask( newTasks );
newTasks = next;
}
}
private void enqueueTask( ScheduledJobHandle newTasks )
{
newTasks.next = null; // Assigning null helps prevent GC nepotism.
delayedTasks.offer( newTasks );
}
private long scheduleDueTasks( long now )
{
if ( delayedTasks.isEmpty() )
{
// We have no tasks to run. Park until we're woken up by a submit().
return NO_TASKS_PARK;
}
ScheduledJobHandle due = spliceOutDueTasks( now );
submitAndEnqueueTasks( due, now );
return delayedTasks.isEmpty() ? NO_TASKS_PARK : delayedTasks.peek().nextDeadlineNanos - now;
}
private ScheduledJobHandle spliceOutDueTasks( long now )
{
ScheduledJobHandle due = null;
while ( !delayedTasks.isEmpty() && delayedTasks.peek().nextDeadlineNanos <= now )
{
ScheduledJobHandle task = delayedTasks.poll();
task.next = due;
due = task;
}
return due;
}
private void submitAndEnqueueTasks( ScheduledJobHandle due, long now )
{
while ( due != null )
{
// Make sure to grab the 'next' reference before any call to enqueueTask.
ScheduledJobHandle next = due.next;
if ( due.compareAndSetState( ScheduledJobHandle.STATE_RUNNABLE, ScheduledJobHandle.STATE_SUBMITTED ) )
{
long reschedulingDelayNanos = due.getReschedulingDelayNanos();
due.nextDeadlineNanos = reschedulingDelayNanos + now;
due.submitTo( pools );
if ( reschedulingDelayNanos > 0 )
{
enqueueTask( due );
}
// If the rescheduling delay is zero or less, then this wasn't a recurring task, but just a delayed one,
// which means we don't enqueue it again.
}
else if ( due.getState() != ScheduledJobHandle.STATE_FAILED )
{
// It's still running, so it's now overdue.
due.nextDeadlineNanos = now;
enqueueTask( due );
}
// Otherwise it's failed, in which case we just throw it away, and continue processing the chain.
due = next;
}
}
@Override
public void run()
{
timeKeeper = Thread.currentThread();
while ( !stopped )
{
long timeToNextTickNanos = tick();
if ( inbox.get() == END_SENTINEL )
{
// Only park if nothing has been posted to our inbox while we were processing the last tick.
LockSupport.parkNanos( this, timeToNextTickNanos );
}
}
}
public void stop()
{
stopped = true;
LockSupport.unpark( timeKeeper );
}
}