blob: 182aee04d526b7d1dad1db0d5f43093ecc96880e [file] [log] [blame]
Madan Jampani05833872016-07-12 23:01:39 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani05833872016-07-12 23:01:39 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.core.impl;
18
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.onosproject.core.HybridLogicalClockService;
20import org.onosproject.core.HybridLogicalTime;
21import org.osgi.service.component.annotations.Activate;
22import org.osgi.service.component.annotations.Component;
23import org.osgi.service.component.annotations.Deactivate;
24import org.slf4j.Logger;
Madan Jampani05833872016-07-12 23:01:39 -070025
26import java.util.function.Supplier;
27
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani05833872016-07-12 23:01:39 -070029
30/**
31 * Implementation of {@link HybridLogicalClockService}.
32 * <p>
33 * Implementation is based on HLT <a href="http://www.cse.buffalo.edu/tech-reports/2014-04.pdf">paper</a>.
34 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035@Component(immediate = true, service = HybridLogicalClockService.class)
Madan Jampani05833872016-07-12 23:01:39 -070036public class HybridLogicalClockManager implements HybridLogicalClockService {
37
38 private final Logger log = getLogger(getClass());
39
40 protected Supplier<Long> physicalTimeSource = System::currentTimeMillis;
41
42 private long logicalTime = 0;
43 private long logicalCounter = 0;
44
45 @Activate
46 public void activate() {
47 log.info("Started");
48 }
49
50 @Deactivate
51 public void deactivate() {
52 log.info("Stopped");
53 }
54
55 @Override
56 public synchronized HybridLogicalTime timeNow() {
57 final long oldLogicalTime = logicalTime;
58 logicalTime = Math.max(oldLogicalTime, physicalTimeSource.get());
59 if (logicalTime == oldLogicalTime) {
60 logicalCounter++;
61 } else {
62 logicalCounter = 0;
63 }
64 return new HybridLogicalTime(logicalTime, logicalCounter);
65 }
66
67 @Override
68 public synchronized void recordEventTime(HybridLogicalTime eTime) {
69 final long oldLogicalTime = logicalTime;
70 logicalTime = Math.max(oldLogicalTime, Math.max(eTime.logicalTime(), physicalTimeSource.get()));
71 if (logicalTime == oldLogicalTime && oldLogicalTime == eTime.logicalTime()) {
72 logicalCounter = Math.max(logicalCounter, eTime.logicalCounter()) + 1;
73 } else if (logicalTime == oldLogicalTime) {
74 logicalCounter++;
75 } else if (logicalTime == eTime.logicalTime()) {
76 logicalCounter = eTime.logicalCounter() + 1;
77 } else {
78 logicalCounter = 0;
79 }
80 }
81
82 protected long logicalTime() {
83 return logicalTime;
84 }
85
86 protected long logicalCounter() {
87 return logicalCounter;
88 }
89}