Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cbf0d8e6e391d1a157d821a7074aeda4794db7f8 (plain) (blame)
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
/*
 * Copyright (c) 2011-2013, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Caspar De Groot - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.server;

import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.spi.server.InternalRepository;

import org.eclipse.net4j.util.ReflectUtil.ExcludeFromDump;
import org.eclipse.net4j.util.concurrent.ConcurrencyUtil;
import org.eclipse.net4j.util.om.monitor.OMMonitor;

import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Bug 297940, 290032
 *
 * @author Caspar De Groot
 */
class TimeStampAuthority
{
  private InternalRepository repository;

  /**
   * Holds the <i>begin</i> timestamp that was issued in response to the last call to {@link #startCommit(long, OMMonitor)}.
   * <p>
   * Synchronized on <code>TimeStampAuthority.this</code>.
   */
  @ExcludeFromDump
  private transient long lastIssuedTimeStamp = CDOBranchPoint.UNSPECIFIED_DATE;

  /**
   * Holds the <i>begin</i> timestamp that was last reported finished by a call to {@link #endCommit(long)}.
   * <p>
   * Synchronized on <code>lastFinishedTimeStampLock</code>.
   */
  private long lastFinishedTimeStamp;

  private LastCommitTimeStampLock lastFinishedTimeStampLock = new LastCommitTimeStampLock();

  private boolean strictOrdering; // TODO (CD) Should be a repo property

  /**
   * A lock to block on if strict commit ordering is enabled
   */
  private StrictOrderingLock strictOrderingLock = new StrictOrderingLock();

  /**
   * An ordered list of timestamps that have been issued but have not (yet) been reported finished. (It is ordered
   * because the timestamps are added sequentially.)
   */
  private List<Long> runningTransactions = new LinkedList<Long>();

  /**
   * A set of timestamps that have been reported finished but have not yet been
   */
  private SortedSet<Long> finishedTransactions = new TreeSet<Long>();

  TimeStampAuthority(InternalRepository repository)
  {
    this.repository = repository;
  }

  /**
   * The purpose of this method is to make sure that no commit can occur at the same time as
   * the base of a new branch. Otherwise that commit could change revisions of that branch base.
   * See bug 506768 and bug 383602.
   */
  synchronized long getMaxBaseTimeForNewBranch()
  {
    long now = repository.getTimeStamp();
    while (repository.getTimeStamp() == now)
    {
      ConcurrencyUtil.sleep(1);
    }

    return now;
  }

  /**
   * @deprecated Not used anymore.
   */
  @Deprecated
  synchronized long[] startCommit(OMMonitor monitor)
  {
    return startCommit(CDOBranchPoint.UNSPECIFIED_DATE, monitor);
  }

  synchronized long[] startCommit(long timeStampOverride, OMMonitor monitor)
  {
    monitor.begin();
    lockIfNeeded();

    try
    {
      long now = repository.getTimeStamp();
      if (lastIssuedTimeStamp != CDOBranchPoint.UNSPECIFIED_DATE)
      {
        while (lastIssuedTimeStamp == now)
        {
          ConcurrencyUtil.sleep(1);
          now = repository.getTimeStamp();
          monitor.checkCanceled();
        }
      }

      if (timeStampOverride != CDOBranchPoint.UNSPECIFIED_DATE)
      {
        now = timeStampOverride;
      }

      long previousTimeStamp = lastIssuedTimeStamp;
      lastIssuedTimeStamp = now;

      runningTransactions.add(now);
      return new long[] { now, previousTimeStamp };
    }
    finally
    {
      monitor.done();
    }
  }

  synchronized void endCommit(long timeStamp)
  {
    if (!runningTransactions.remove(timeStamp))
    {
      throw new IllegalArgumentException("Cannot end transaction with unknown timestamp " + timeStamp);
    }

    finishedTransactions.add(timeStamp);

    // We can remove a timestamp from finishedTransactions if it is smaller (i.e. older) than any
    // of the runningTransactions. Since both sets are sorted, we only need to compare the heads.
    long oldestRunning = runningTransactions.isEmpty() ? Long.MAX_VALUE : runningTransactions.get(0);
    long oldestFinished;
    synchronized (lastFinishedTimeStampLock)
    {
      long oldValue = lastFinishedTimeStamp;
      while (!finishedTransactions.isEmpty() && (oldestFinished = finishedTransactions.first()) < oldestRunning)
      {
        finishedTransactions.remove(oldestFinished);
        setLastFinishedTimeStampUnsynced(oldestFinished);
      }

      // If we actually changed the lastFinishedTimeStamp, we need to notify waiting threads
      if (lastFinishedTimeStamp != oldValue)
      {
        lastFinishedTimeStampLock.notifyAll();
      }
    }

    unlockIfNeeded();
  }

  synchronized void failCommit(long timeStamp)
  {
    // Exclude problems before TransactionCommitContext.setTimeStamp()
    if (timeStamp == CDOBranchPoint.UNSPECIFIED_DATE)
    {
      unlockIfNeeded();
    }
    else
    {
      endCommit(timeStamp);
    }
  }

  synchronized long getLastFinishedTimeStamp()
  {
    if (lastFinishedTimeStamp != 0)
    {
      return lastFinishedTimeStamp;
    }

    // If we get here, no commit has finished since the server was started
    if (lastIssuedTimeStamp == 0) // No commit has started either
    {
      // We can safely return the current system time minus one milli.
      return repository.getTimeStamp() - 1;
    }

    // If we get here, one or more commits are running
    // We can safely return the start time of the longest-running, minus one milli.
    return runningTransactions.get(0) - 1;
  }

  long waitForCommit(long timeout)
  {
    synchronized (lastFinishedTimeStampLock)
    {
      try
      {
        lastFinishedTimeStampLock.wait(timeout);
      }
      catch (Exception ignore)
      {
      }

      return lastFinishedTimeStamp;
    }
  }

  void setLastFinishedTimeStamp(long lastCommitTimeStamp)
  {
    synchronized (lastFinishedTimeStampLock)
    {
      if (lastCommitTimeStamp > lastFinishedTimeStamp)
      {
        lastIssuedTimeStamp = lastCommitTimeStamp;
        setLastFinishedTimeStampUnsynced(lastCommitTimeStamp);
        lastFinishedTimeStampLock.notifyAll();
      }
    }
  }

  private void setLastFinishedTimeStampUnsynced(long lastCommitTimeStamp)
  {
    lastFinishedTimeStamp = lastCommitTimeStamp;
    repository.getStore().setLastCommitTime(lastFinishedTimeStamp);
  }

  private void lockIfNeeded()
  {
    if (strictOrdering)
    {
      strictOrderingLock.lock();
    }
  }

  private void unlockIfNeeded()
  {
    if (strictOrdering)
    {
      strictOrderingLock.unlock();
    }
  }

  /**
   * A separate class for better monitor debugging.
   *
   * @author Eike Stepper
   */
  private static final class LastCommitTimeStampLock
  {
  }

  /**
   * A separate class for better monitor debugging.
   *
   * @author Eike Stepper
   */
  private static final class StrictOrderingLock extends ReentrantLock
  {
    private static final long serialVersionUID = 1L;
  }
}

Back to the top