Madan Jampani

Log failures in state machine processing

Change-Id: Ib92768cf4cf5cce5e2642265d1c1aa3e2f13b246
...@@ -259,36 +259,41 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements Se ...@@ -259,36 +259,41 @@ public class AtomixConsistentMapState extends ResourceStateMachine implements Se
259 * @return update result 259 * @return update result
260 */ 260 */
261 protected MapEntryUpdateResult<String, byte[]> updateAndGet(Commit<? extends UpdateAndGet> commit) { 261 protected MapEntryUpdateResult<String, byte[]> updateAndGet(Commit<? extends UpdateAndGet> commit) {
262 - MapEntryUpdateResult.Status updateStatus = validate(commit.operation()); 262 + try {
263 - String key = commit.operation().key(); 263 + MapEntryUpdateResult.Status updateStatus = validate(commit.operation());
264 - MapEntryValue oldCommitValue = mapEntries.get(commit.operation().key()); 264 + String key = commit.operation().key();
265 - Versioned<byte[]> oldMapValue = toVersioned(oldCommitValue); 265 + MapEntryValue oldCommitValue = mapEntries.get(commit.operation().key());
266 + Versioned<byte[]> oldMapValue = toVersioned(oldCommitValue);
266 267
267 - if (updateStatus != MapEntryUpdateResult.Status.OK) { 268 + if (updateStatus != MapEntryUpdateResult.Status.OK) {
268 - commit.close(); 269 + commit.close();
269 - return new MapEntryUpdateResult<>(updateStatus, "", key, 270 + return new MapEntryUpdateResult<>(updateStatus, "", key,
270 - oldMapValue, oldMapValue); 271 + oldMapValue, oldMapValue);
271 - } 272 + }
272 273
273 - byte[] newValue = commit.operation().value(); 274 + byte[] newValue = commit.operation().value();
274 - long newVersion = versionCounter.incrementAndGet(); 275 + long newVersion = versionCounter.incrementAndGet();
275 - Versioned<byte[]> newMapValue = newValue == null ? null 276 + Versioned<byte[]> newMapValue = newValue == null ? null
276 - : new Versioned<>(newValue, newVersion); 277 + : new Versioned<>(newValue, newVersion);
277 278
278 - MapEvent.Type updateType = newValue == null ? REMOVE 279 + MapEvent.Type updateType = newValue == null ? REMOVE
279 - : oldCommitValue == null ? INSERT : UPDATE; 280 + : oldCommitValue == null ? INSERT : UPDATE;
280 - if (updateType == REMOVE || updateType == UPDATE) { 281 + if (updateType == REMOVE || updateType == UPDATE) {
281 - mapEntries.remove(key); 282 + mapEntries.remove(key);
282 - oldCommitValue.discard(); 283 + oldCommitValue.discard();
283 - } 284 + }
284 - if (updateType == INSERT || updateType == UPDATE) { 285 + if (updateType == INSERT || updateType == UPDATE) {
285 - mapEntries.put(key, new NonTransactionalCommit(newVersion, commit)); 286 + mapEntries.put(key, new NonTransactionalCommit(newVersion, commit));
286 - } else { 287 + } else {
287 - commit.close(); 288 + commit.close();
289 + }
290 + publish(Lists.newArrayList(new MapEvent<>("", key, newMapValue, oldMapValue)));
291 + return new MapEntryUpdateResult<>(updateStatus, "", key, oldMapValue,
292 + newMapValue);
293 + } catch (Exception e) {
294 + log.error("State machine operation failed", e);
295 + throw Throwables.propagate(e);
288 } 296 }
289 - publish(Lists.newArrayList(new MapEvent<>("", key, newMapValue, oldMapValue)));
290 - return new MapEntryUpdateResult<>(updateStatus, "", key, oldMapValue,
291 - newMapValue);
292 } 297 }
293 298
294 /** 299 /**
......
...@@ -58,6 +58,7 @@ import org.slf4j.Logger; ...@@ -58,6 +58,7 @@ import org.slf4j.Logger;
58 58
59 import com.google.common.base.MoreObjects; 59 import com.google.common.base.MoreObjects;
60 import com.google.common.base.Objects; 60 import com.google.common.base.Objects;
61 +import com.google.common.base.Throwables;
61 import com.google.common.collect.Lists; 62 import com.google.common.collect.Lists;
62 import com.google.common.collect.Maps; 63 import com.google.common.collect.Maps;
63 64
...@@ -170,6 +171,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -170,6 +171,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
170 notifyLeadershipChange(oldLeadership, newLeadership); 171 notifyLeadershipChange(oldLeadership, newLeadership);
171 } 172 }
172 return newLeadership; 173 return newLeadership;
174 + } catch (Exception e) {
175 + log.error("State machine operation failed", e);
176 + throw Throwables.propagate(e);
173 } finally { 177 } finally {
174 commit.close(); 178 commit.close();
175 } 179 }
...@@ -189,6 +193,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -189,6 +193,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
189 if (!Objects.equal(oldLeadership, newLeadership)) { 193 if (!Objects.equal(oldLeadership, newLeadership)) {
190 notifyLeadershipChange(oldLeadership, newLeadership); 194 notifyLeadershipChange(oldLeadership, newLeadership);
191 } 195 }
196 + } catch (Exception e) {
197 + log.error("State machine operation failed", e);
198 + throw Throwables.propagate(e);
192 } finally { 199 } finally {
193 commit.close(); 200 commit.close();
194 } 201 }
...@@ -213,6 +220,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -213,6 +220,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
213 return (electionState != null && 220 return (electionState != null &&
214 electionState.leader() != null && 221 electionState.leader() != null &&
215 commit.operation().nodeId().equals(electionState.leader().nodeId())); 222 commit.operation().nodeId().equals(electionState.leader().nodeId()));
223 + } catch (Exception e) {
224 + log.error("State machine operation failed", e);
225 + throw Throwables.propagate(e);
216 } finally { 226 } finally {
217 commit.close(); 227 commit.close();
218 } 228 }
...@@ -237,6 +247,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -237,6 +247,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
237 notifyLeadershipChange(oldLeadership, newLeadership); 247 notifyLeadershipChange(oldLeadership, newLeadership);
238 } 248 }
239 return true; 249 return true;
250 + } catch (Exception e) {
251 + log.error("State machine operation failed", e);
252 + throw Throwables.propagate(e);
240 } finally { 253 } finally {
241 commit.close(); 254 commit.close();
242 } 255 }
...@@ -260,6 +273,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -260,6 +273,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
260 } 273 }
261 }); 274 });
262 notifyLeadershipChanges(changes); 275 notifyLeadershipChanges(changes);
276 + } catch (Exception e) {
277 + log.error("State machine operation failed", e);
278 + throw Throwables.propagate(e);
263 } finally { 279 } finally {
264 commit.close(); 280 commit.close();
265 } 281 }
...@@ -274,6 +290,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -274,6 +290,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
274 String topic = commit.operation().topic(); 290 String topic = commit.operation().topic();
275 try { 291 try {
276 return leadership(topic); 292 return leadership(topic);
293 + } catch (Exception e) {
294 + log.error("State machine operation failed", e);
295 + throw Throwables.propagate(e);
277 } finally { 296 } finally {
278 commit.close(); 297 commit.close();
279 } 298 }
...@@ -291,6 +310,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -291,6 +310,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
291 Leader leader = leadership(e.getKey()).leader(); 310 Leader leader = leadership(e.getKey()).leader();
292 return leader != null && leader.nodeId().equals(nodeId); 311 return leader != null && leader.nodeId().equals(nodeId);
293 }).keySet(); 312 }).keySet();
313 + } catch (Exception e) {
314 + log.error("State machine operation failed", e);
315 + throw Throwables.propagate(e);
294 } finally { 316 } finally {
295 commit.close(); 317 commit.close();
296 } 318 }
...@@ -306,6 +328,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine ...@@ -306,6 +328,9 @@ public class AtomixLeaderElectorState extends ResourceStateMachine
306 try { 328 try {
307 result.putAll(Maps.transformEntries(elections, (k, v) -> leadership(k))); 329 result.putAll(Maps.transformEntries(elections, (k, v) -> leadership(k)));
308 return result; 330 return result;
331 + } catch (Exception e) {
332 + log.error("State machine operation failed", e);
333 + throw Throwables.propagate(e);
309 } finally { 334 } finally {
310 commit.close(); 335 commit.close();
311 } 336 }
......