5
5
package oracle .kubernetes .operator .helpers ;
6
6
7
7
import io .kubernetes .client .models .V1EnvVar ;
8
+ import io .kubernetes .client .models .V1ObjectMeta ;
8
9
import io .kubernetes .client .models .V1Service ;
9
10
import java .util .Collection ;
10
11
import java .util .Collections ;
11
12
import java .util .List ;
13
+ import java .util .Objects ;
12
14
import java .util .Optional ;
13
15
import java .util .concurrent .ConcurrentHashMap ;
14
16
import java .util .concurrent .ConcurrentMap ;
15
17
import java .util .concurrent .atomic .AtomicBoolean ;
16
18
import java .util .concurrent .atomic .AtomicInteger ;
17
19
import java .util .concurrent .atomic .AtomicReference ;
20
+ import java .util .function .Predicate ;
18
21
import oracle .kubernetes .operator .wlsconfig .WlsServerConfig ;
19
22
import oracle .kubernetes .weblogic .domain .model .Domain ;
20
23
import oracle .kubernetes .weblogic .domain .model .ServerSpec ;
21
- import org .joda .time .DateTime ;
22
24
23
25
/**
24
26
* Operator's mapping between custom resource Domain and runtime details about that domain,
@@ -36,8 +38,6 @@ public class DomainPresenceInfo {
36
38
private final ConcurrentMap <String , ServerKubernetesObjects > servers = new ConcurrentHashMap <>();
37
39
private final ConcurrentMap <String , V1Service > clusters = new ConcurrentHashMap <>();
38
40
39
- private DateTime lastCompletionTime ;
40
-
41
41
/**
42
42
* Create presence for a domain.
43
43
*
@@ -71,28 +71,95 @@ private ServerKubernetesObjects getSko(String serverName) {
71
71
return getServers ().computeIfAbsent (serverName , (n -> new ServerKubernetesObjects ()));
72
72
}
73
73
74
- V1Service getServerService (String serverName ) {
74
+ public V1Service getServerService (String serverName ) {
75
75
return getSko (serverName ).getService ().get ();
76
76
}
77
77
78
+ void setServerServiceFromEvent (String serverName , V1Service event ) {
79
+ getSko (serverName ).getService ().accumulateAndGet (event , this ::getNewerService );
80
+ }
81
+
82
+ boolean deleteServerServiceFromEvent (String serverName , V1Service event ) {
83
+ if (serverName == null ) return false ;
84
+ V1Service deletedService =
85
+ getSko (serverName ).getService ().getAndAccumulate (event , this ::getNewerCurrentOrNull );
86
+ return deletedService != null ;
87
+ }
88
+
89
+ /**
90
+ * Computes the result of a delete attempt. If the current service is newer than the one
91
+ * associated with the delete event, returns it; otherwise returns null, thus deleting the value.
92
+ *
93
+ * @param service the current service
94
+ * @param event the service associated with the delete event
95
+ * @return the new value for the service.
96
+ */
97
+ private V1Service getNewerCurrentOrNull (V1Service service , V1Service event ) {
98
+ return KubernetesUtils .isFirstNewer (getMetadata (service ), getMetadata (event )) ? service : null ;
99
+ }
100
+
78
101
void removeClusterService (String clusterName ) {
79
- getClusters () .remove (clusterName );
102
+ clusters .remove (clusterName );
80
103
}
81
104
82
105
V1Service getClusterService (String clusterName ) {
83
- return getClusters ().get (clusterName );
106
+ return clusters .get (clusterName );
107
+ }
108
+
109
+ void setClusterService (String clusterName , V1Service service ) {
110
+ clusters .put (clusterName , service );
111
+ }
112
+
113
+ void setClusterServiceFromEvent (String clusterName , V1Service event ) {
114
+ if (clusterName == null ) return ;
115
+
116
+ clusters .compute (clusterName , (k , s ) -> getNewerService (s , event ));
117
+ }
118
+
119
+ boolean deleteClusterServiceFromEvent (String clusterName , V1Service event ) {
120
+ return removeIfPresentAnd (
121
+ clusters ,
122
+ clusterName ,
123
+ s -> !KubernetesUtils .isFirstNewer (getMetadata (s ), getMetadata (event )));
124
+ }
125
+
126
+ private static <K , V > boolean removeIfPresentAnd (
127
+ ConcurrentMap <K , V > map , K key , Predicate <? super V > predicateFunction ) {
128
+ Objects .requireNonNull (predicateFunction );
129
+ for (V oldValue ; (oldValue = map .get (key )) != null ; ) {
130
+ if (!predicateFunction .test (oldValue )) return false ;
131
+ else if (map .remove (key , oldValue )) return true ;
132
+ }
133
+ return false ;
84
134
}
85
135
86
- public void setClusterService (String clusterName , V1Service service ) {
87
- getClusters ().put (clusterName , service );
136
+ private V1Service getNewerService (V1Service first , V1Service second ) {
137
+ return KubernetesUtils .isFirstNewer (getMetadata (first ), getMetadata (second )) ? first : second ;
138
+ }
139
+
140
+ private V1ObjectMeta getMetadata (V1Service service ) {
141
+ return service == null ? null : service .getMetadata ();
88
142
}
89
143
90
144
V1Service getExternalService (String serverName ) {
91
- return getSko (serverName ).getExternalService ();
145
+ return getSko (serverName ).getExternalService (). get () ;
92
146
}
93
147
94
148
void setExternalService (String serverName , V1Service service ) {
95
- getSko (serverName ).setExternalService (service );
149
+ getSko (serverName ).getExternalService ().set (service );
150
+ }
151
+
152
+ void setExternalServiceFromEvent (String serverName , V1Service event ) {
153
+ getSko (serverName ).getExternalService ().accumulateAndGet (event , this ::getNewerService );
154
+ }
155
+
156
+ boolean deleteExternalServiceFromEvent (String serverName , V1Service event ) {
157
+ if (serverName == null ) return false ;
158
+ V1Service deletedService =
159
+ getSko (serverName )
160
+ .getExternalService ()
161
+ .getAndAccumulate (event , this ::getNewerCurrentOrNull );
162
+ return deletedService != null ;
96
163
}
97
164
98
165
public boolean isDeleting () {
@@ -111,30 +178,20 @@ public void setPopulated(boolean populated) {
111
178
isPopulated .set (populated );
112
179
}
113
180
114
- public void resetFailureCount () {
181
+ private void resetFailureCount () {
115
182
retryCount .set (0 );
116
183
}
117
184
118
185
public int incrementAndGetFailureCount () {
119
186
return retryCount .incrementAndGet ();
120
187
}
121
188
122
- public int getRetryCount () {
189
+ int getRetryCount () {
123
190
return retryCount .get ();
124
191
}
125
192
126
- /**
127
- * Last completion time.
128
- *
129
- * @return Last completion time
130
- */
131
- public DateTime getLastCompletionTime () {
132
- return lastCompletionTime ;
133
- }
134
-
135
193
/** Sets the last completion time to now. */
136
194
public void complete () {
137
- this .lastCompletionTime = new DateTime ();
138
195
resetFailureCount ();
139
196
}
140
197
@@ -183,15 +240,6 @@ public ConcurrentMap<String, ServerKubernetesObjects> getServers() {
183
240
return servers ;
184
241
}
185
242
186
- /**
187
- * Map from cluster name to Service objects.
188
- *
189
- * @return Cluster object map
190
- */
191
- public ConcurrentMap <String , V1Service > getClusters () {
192
- return clusters ;
193
- }
194
-
195
243
/**
196
244
* Server startup info.
197
245
*
0 commit comments