Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / PersistenceProtocol.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.access.commands;
9
10 import java.io.DataInput;
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.concepts.WritableObject;
15
16 /**
17  * Enumeration of transaction persistence protocols. These govern which protocol is executed between the frontend
18  * and backend to drive persistence of a particular transaction.
19  */
20 public enum PersistenceProtocol implements WritableObject {
21     /**
22      * Abort protocol. The transaction has been aborted on the frontend and its effects should not be visible
23      * in the global history. This is a simple request/reply protocol.
24      */
25     ABORT {
26         @Override
27         byte byteValue() {
28             return 1;
29         }
30     },
31     /**
32      * Simple commit protocol. The transaction should be committed to the global history. The receiving backend
33      * is the only entity which needs to persist its effects, hence a simple request/reply protocol is sufficient.
34      */
35     SIMPLE {
36         @Override
37         byte byteValue() {
38             return 2;
39         }
40     },
41     /**
42      * Three-phase commit protocol (3PC). The transaction should be committed to the global history, but it is a part
43      * of a transaction spanning multiple entities and coordination is needed to drive persistence.
44      */
45     THREE_PHASE {
46         @Override
47         byte byteValue() {
48             return 3;
49         }
50     },
51     /**
52      * Transaction is ready. This is not a really a persistence protocol, but an indication that frontend has
53      * completed modifications on the transaction and considers it ready, without deciding the actual commit protocol.
54      */
55     READY {
56         @Override
57         byte byteValue() {
58             return 4;
59         }
60     };
61
62     @Override
63     public final void writeTo(final DataOutput out) throws IOException {
64         out.writeByte(byteValue());
65     }
66
67     public static PersistenceProtocol readFrom(final DataInput in) throws IOException {
68         return valueOf(in.readByte());
69     }
70
71     abstract byte byteValue();
72
73     static int byteValue(final PersistenceProtocol finish) {
74         return finish == null ? 0 : finish.byteValue();
75     }
76
77     static @Nullable PersistenceProtocol valueOf(final byte value) {
78         return switch (value) {
79             case 0 -> null;
80             case 1 -> ABORT;
81             case 2 -> SIMPLE;
82             case 3 -> THREE_PHASE;
83             case 4 -> READY;
84             default -> throw new IllegalArgumentException("Unhandled byte value " + value);
85         };
86     }
87 }