Jonathan Hart

Initial implementation of EventuallyConsistentMap.

The map uses the gossip schemes to replicate data between instances. It seems
to work for basic add and remove use cases right now, no anti-entropy yet.

ONOS-844.

Change-Id: I7d05a7b532e40c95ab14e2c8911f18514bd0a8ca
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 +package org.onosproject.store.impl;
17 +
18 +import org.onosproject.store.Timestamp;
19 +
20 +/**
21 + * Clock service that can generate timestamps per object.
22 + */
23 +public interface ClockService<T> {
24 +
25 + /**
26 + * Gets a new timestamp for the given object.
27 + *
28 + * @param object Object to get a timestamp for
29 + * @return the new timestamp
30 + */
31 + public Timestamp getTimestamp(T object);
32 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 +package org.onosproject.store.impl;
17 +
18 +import java.util.Collection;
19 +import java.util.Map;
20 +import java.util.Set;
21 +
22 +/**
23 + * A distributed, eventually consistent map.
24 + *
25 + * This map does not offer read after writes consistency. Operations are
26 + * serialized via the timestamps issued by the clock service. If two updates
27 + * are in conflict, the update with the more recent timestamp will endure.
28 + *
29 + * The interface is mostly similar to {@link java.util.Map} with some minor
30 + * semantic changes and the addition of a listener framework (because the map
31 + * can be mutated by clients on other instances, not only through the local Java
32 + * API).
33 + *
34 + * Clients are expected to register an
35 + * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if they
36 + * are interested in receiving notifications of update to the map.
37 + */
38 +public interface EventuallyConsistentMap<K, V> {
39 +
40 + /**
41 + * Returns the number of key-value mappings in this map.
42 + *
43 + * @return number of key-value mappings
44 + */
45 + public int size();
46 +
47 + /**
48 + * Returns true if this map is empty.
49 + *
50 + * @return true if this map is empty, otherwise false
51 + */
52 + public boolean isEmpty();
53 +
54 + /**
55 + * Returns true if the map contains a mapping for the specified key.
56 + *
57 + * @param key the key to check if this map contains
58 + * @return true if this map has a mapping for the key, otherwise false
59 + */
60 + public boolean containsKey(K key);
61 +
62 + /**
63 + * Returns true if the map contains a mapping from any key to the specified
64 + * value.
65 + *
66 + * @param value the value to check if this map has a mapping for
67 + * @return true if this map has a mapping to this value, otherwise false
68 + */
69 + public boolean containsValue(V value);
70 +
71 + /**
72 + * Returns the value mapped to the specified key.
73 + *
74 + * @param key the key to look up in this map
75 + * @return the value mapped to the key, or null if no mapping is found
76 + */
77 + public V get(K key);
78 +
79 + /**
80 + * Associates the specified value to the specified key in this map.
81 + * <p>
82 + * Note: this differs from the specification of {@link java.util.Map}
83 + * because it does not return the previous value associated with the key.
84 + * Clients are expected to register an
85 + * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if
86 + * they are interested in receiving notification of updates to the map.
87 + * </p>
88 + *
89 + * @param key the key to add a mapping for in this map
90 + * @param value the value to associate with the key in this map
91 + */
92 + public void put(K key, V value);
93 +
94 + /**
95 + * Removes the mapping associated with the specified key from the map.
96 + * <p>
97 + * Note: this differs from the specification of {@link java.util.Map}
98 + * because it does not return the previous value associated with the key.
99 + * Clients are expected to register an
100 + * {@link org.onosproject.store.impl.EventuallyConsistentMapListener} if
101 + * they are interested in receiving notification of updates to the map.
102 + * </p>
103 + *
104 + * @param key the key to remove the mapping for
105 + */
106 + public void remove(K key);
107 +
108 + /**
109 + * Adds mappings for all key-value pairs in the specified map to this map.
110 + * <p>
111 + * This will be more efficient in communication than calling individual put
112 + * operations.
113 + * </p>
114 + *
115 + * @param m a map of values to add to this map
116 + */
117 + public void putAll(Map<? extends K, ? extends V> m);
118 +
119 + /**
120 + * Removes all mappings from this map.
121 + */
122 + public void clear();
123 +
124 + /**
125 + * Returns a set of the keys in this map. Changes to the set are not
126 + * reflected back to the map.
127 + *
128 + * @return set of keys in the map
129 + */
130 + public Set<K> keySet();
131 +
132 + /**
133 + * Returns a collections of values in this map. Changes to the collection
134 + * are not reflected back to the map.
135 + *
136 + * @return collection of values in the map
137 + */
138 + public Collection<V> values();
139 +
140 + /**
141 + * Returns a set of mappings contained in this map. Changes to the set are
142 + * not reflected back to the map.
143 + *
144 + * @return set of key-value mappings in this map
145 + */
146 + public Set<Map.Entry<K, V>> entrySet();
147 +
148 + /**
149 + * Adds the specified listener to the map which will be notified whenever
150 + * the mappings in the map are changed.
151 + *
152 + * @param listener listener to register for events
153 + */
154 + public void addListener(EventuallyConsistentMapListener listener);
155 +
156 + /**
157 + * Removes the specified listener from the map such that it will no longer
158 + * receive change notifications.
159 + *
160 + * @param listener listener to deregister for events
161 + */
162 + public void removeListener(EventuallyConsistentMapListener listener);
163 +
164 + /**
165 + * Shuts down the map and breaks communication between different instances.
166 + * This allows the map objects to be cleaned up and garbage collected.
167 + * Calls to any methods on the map subsequent to calling destroy() will
168 + * throw a {@link java.lang.RuntimeException}.
169 + */
170 + public void destroy();
171 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 +package org.onosproject.store.impl;
17 +
18 +/**
19 + * Event object signalling that the map was modified.
20 + */
21 +public class EventuallyConsistentMapEvent<K, V> {
22 +
23 + public enum Type {
24 + PUT,
25 + REMOVE
26 + }
27 +
28 + private final Type type;
29 + private final K key;
30 + private final V value;
31 +
32 + /**
33 + * Creates a new event object.
34 + *
35 + * @param type the type of the event
36 + * @param key the key the event concerns
37 + * @param value the value related to the key, or null for remove events
38 + */
39 + public EventuallyConsistentMapEvent(Type type, K key, V value) {
40 + this.type = type;
41 + this.key = key;
42 + this.value = value;
43 + }
44 +
45 + /**
46 + * Returns the type of the event.
47 + *
48 + * @return the type of the event
49 + */
50 + public Type type() {
51 + return type;
52 + }
53 +
54 + /**
55 + * Returns the key this event concerns.
56 + *
57 + * @return the key
58 + */
59 + public K key() {
60 + return key;
61 + }
62 +
63 + /**
64 + * Returns the value associated with this event.
65 + *
66 + * @return the value, or null if the event was REMOVE
67 + */
68 + public V value() {
69 + return value;
70 + }
71 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 +package org.onosproject.store.impl;
17 +
18 +/**
19 + * Listener interested in receiving modification events for an
20 + * EventuallyConsistentMap.
21 + */
22 +public interface EventuallyConsistentMapListener {
23 +
24 + /**
25 + * Reacts to the specified event.
26 + *
27 + * @param event the event
28 + */
29 + public void event(EventuallyConsistentMapEvent event);
30 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
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 +package org.onosproject.store.impl;
17 +
18 +import org.onosproject.store.Timestamp;
19 +
20 +/**
21 + * A clock service which hands out wallclock-based timestamps.
22 + */
23 +public class WallclockClockManager<T> implements ClockService<T> {
24 + @Override
25 + public Timestamp getTimestamp(T object) {
26 + return new WallClockTimestamp();
27 + }
28 +}