|
| 1 | +/*- |
| 2 | + * Copyright (c) 2011, 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 5 | + * https://oss.oracle.com/licenses/upl/ |
| 6 | + */ |
| 7 | + |
| 8 | +package oracle.nosql.driver; |
| 9 | + |
| 10 | +/** |
| 11 | + * Defines the durability characteristics associated with a standalone write |
| 12 | + * (put or update) operation. |
| 13 | + * <p> |
| 14 | + * This is currently only supported in On-Prem installations. It is ignored |
| 15 | + * in the cloud service. |
| 16 | + * <p> |
| 17 | + * The overall durability is a function of the {@link SyncPolicy} and {@link |
| 18 | + * ReplicaAckPolicy} in effect for the Master, and the {@link SyncPolicy} in |
| 19 | + * effect for each Replica. |
| 20 | + * </p> |
| 21 | + * |
| 22 | + * @since 5.3.0 |
| 23 | + */ |
| 24 | +public class Durability { |
| 25 | + |
| 26 | + /** |
| 27 | + * A convenience constant that defines a durability policy with COMMIT_SYNC |
| 28 | + * for Master commit synchronization. |
| 29 | + * |
| 30 | + * The policies default to COMMIT_NO_SYNC for commits of replicated |
| 31 | + * transactions that need acknowledgment and SIMPLE_MAJORITY for the |
| 32 | + * acknowledgment policy. |
| 33 | + */ |
| 34 | + public static final Durability COMMIT_SYNC = |
| 35 | + new Durability(SyncPolicy.SYNC, |
| 36 | + SyncPolicy.NO_SYNC, |
| 37 | + ReplicaAckPolicy.SIMPLE_MAJORITY); |
| 38 | + |
| 39 | + /** |
| 40 | + * A convenience constant that defines a durability policy with |
| 41 | + * COMMIT_NO_SYNC for Master commit synchronization. |
| 42 | + * |
| 43 | + * The policies default to COMMIT_NO_SYNC for commits of replicated |
| 44 | + * transactions that need acknowledgment and SIMPLE_MAJORITY for the |
| 45 | + * acknowledgment policy. |
| 46 | + */ |
| 47 | + public static final Durability COMMIT_NO_SYNC = |
| 48 | + new Durability(SyncPolicy.NO_SYNC, |
| 49 | + SyncPolicy.NO_SYNC, |
| 50 | + ReplicaAckPolicy.SIMPLE_MAJORITY); |
| 51 | + |
| 52 | + /** |
| 53 | + * A convenience constant that defines a durability policy with |
| 54 | + * COMMIT_WRITE_NO_SYNC for Master commit synchronization. |
| 55 | + * |
| 56 | + * The policies default to COMMIT_NO_SYNC for commits of replicated |
| 57 | + * transactions that need acknowledgment and SIMPLE_MAJORITY for the |
| 58 | + * acknowledgment policy. |
| 59 | + */ |
| 60 | + public static final Durability COMMIT_WRITE_NO_SYNC = |
| 61 | + new Durability(SyncPolicy.WRITE_NO_SYNC, |
| 62 | + SyncPolicy.NO_SYNC, |
| 63 | + ReplicaAckPolicy.SIMPLE_MAJORITY); |
| 64 | + |
| 65 | + /** |
| 66 | + * Defines the synchronization policy to be used when committing a |
| 67 | + * transaction. High levels of synchronization offer a greater guarantee |
| 68 | + * that the transaction is persistent to disk, but trade that off for |
| 69 | + * lower performance. |
| 70 | + */ |
| 71 | + public enum SyncPolicy { |
| 72 | + |
| 73 | + /** |
| 74 | + * Write and synchronously flush the log on transaction commit. |
| 75 | + * Transactions exhibit all the ACID (atomicity, consistency, |
| 76 | + * isolation, and durability) properties. |
| 77 | + */ |
| 78 | + SYNC, |
| 79 | + |
| 80 | + /** |
| 81 | + * Do not write or synchronously flush the log on transaction commit. |
| 82 | + * Transactions exhibit the ACI (atomicity, consistency, and isolation) |
| 83 | + * properties, but not D (durability); that is, database integrity will |
| 84 | + * be maintained, but if the application or system fails, it is |
| 85 | + * possible some number of the most recently committed transactions may |
| 86 | + * be undone during recovery. The number of transactions at risk is |
| 87 | + * governed by how many log updates can fit into the log buffer, how |
| 88 | + * often the operating system flushes dirty buffers to disk, and how |
| 89 | + * often log checkpoints occur. |
| 90 | + */ |
| 91 | + NO_SYNC, |
| 92 | + |
| 93 | + /** |
| 94 | + * Write but do not synchronously flush the log on transaction commit. |
| 95 | + * Transactions exhibit the ACI (atomicity, consistency, and isolation) |
| 96 | + * properties, but not D (durability); that is, database integrity will |
| 97 | + * be maintained, but if the operating system fails, it is possible |
| 98 | + * some number of the most recently committed transactions may be |
| 99 | + * undone during recovery. The number of transactions at risk is |
| 100 | + * governed by how often the operating system flushes dirty buffers to |
| 101 | + * disk, and how often log checkpoints occur. |
| 102 | + */ |
| 103 | + WRITE_NO_SYNC; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * A replicated environment makes it possible to increase an application's |
| 108 | + * transaction commit guarantees by committing changes to its replicas on |
| 109 | + * the network. ReplicaAckPolicy defines the policy for how such network |
| 110 | + * commits are handled. |
| 111 | + */ |
| 112 | + public enum ReplicaAckPolicy { |
| 113 | + |
| 114 | + /** |
| 115 | + * All replicas must acknowledge that they have committed the |
| 116 | + * transaction. This policy should be selected only if your replication |
| 117 | + * group has a small number of replicas, and those replicas are on |
| 118 | + * extremely reliable networks and servers. |
| 119 | + */ |
| 120 | + ALL, |
| 121 | + |
| 122 | + /** |
| 123 | + * No transaction commit acknowledgments are required and the master |
| 124 | + * will never wait for replica acknowledgments. In this case, |
| 125 | + * transaction durability is determined entirely by the type of commit |
| 126 | + * that is being performed on the master. |
| 127 | + */ |
| 128 | + NONE, |
| 129 | + |
| 130 | + /** |
| 131 | + * A simple majority of replicas must acknowledge that they have |
| 132 | + * committed the transaction. This acknowledgment policy, in |
| 133 | + * conjunction with an election policy which requires at least a simple |
| 134 | + * majority, ensures that the changes made by the transaction remains |
| 135 | + * durable if a new election is held. |
| 136 | + */ |
| 137 | + SIMPLE_MAJORITY; |
| 138 | + } |
| 139 | + |
| 140 | + /* The sync policy in effect on the Master node. */ |
| 141 | + private final SyncPolicy masterSync; |
| 142 | + |
| 143 | + /* The sync policy in effect on a replica. */ |
| 144 | + final private SyncPolicy replicaSync; |
| 145 | + |
| 146 | + /* The replica acknowledgment policy to be used. */ |
| 147 | + final private ReplicaAckPolicy replicaAck; |
| 148 | + |
| 149 | + /** |
| 150 | + * Creates an instance of a Durability specification. |
| 151 | + * |
| 152 | + * @param masterSync the SyncPolicy to be used when committing the |
| 153 | + * transaction on the Master. |
| 154 | + * @param replicaSync the SyncPolicy to be used remotely, as part of a |
| 155 | + * transaction acknowledgment, at a Replica node. |
| 156 | + * @param replicaAck the acknowledgment policy used when obtaining |
| 157 | + * transaction acknowledgments from Replicas. |
| 158 | + */ |
| 159 | + public Durability(SyncPolicy masterSync, |
| 160 | + SyncPolicy replicaSync, |
| 161 | + ReplicaAckPolicy replicaAck) { |
| 162 | + this.masterSync = masterSync; |
| 163 | + this.replicaSync = replicaSync; |
| 164 | + this.replicaAck = replicaAck; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String toString() { |
| 169 | + return masterSync.toString() + "," + |
| 170 | + replicaSync.toString() + "," + |
| 171 | + replicaAck.toString(); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns the transaction synchronization policy to be used on the Master |
| 176 | + * when committing a transaction. |
| 177 | + * @return the master transaction synchronization policy |
| 178 | + */ |
| 179 | + public SyncPolicy getMasterSync() { |
| 180 | + return masterSync; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Returns the transaction synchronization policy to be used by the replica |
| 185 | + * as it replays a transaction that needs an acknowledgment. |
| 186 | + * @return the replica transaction synchronization policy |
| 187 | + */ |
| 188 | + public SyncPolicy getReplicaSync() { |
| 189 | + return replicaSync; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Returns the replica acknowledgment policy used by the master when |
| 194 | + * committing changes to a replicated environment. |
| 195 | + * @return the replica acknowledgment policy |
| 196 | + */ |
| 197 | + public ReplicaAckPolicy getReplicaAck() { |
| 198 | + return replicaAck; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public int hashCode() { |
| 203 | + final int prime = 31; |
| 204 | + int result = 1; |
| 205 | + result = (prime * result) + masterSync.hashCode(); |
| 206 | + result = (prime * result) + replicaAck.hashCode(); |
| 207 | + result = (prime * result) + replicaSync.hashCode(); |
| 208 | + return result; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public boolean equals(Object obj) { |
| 213 | + if (this == obj) { |
| 214 | + return true; |
| 215 | + } |
| 216 | + if (obj == null) { |
| 217 | + return false; |
| 218 | + } |
| 219 | + if (!(obj instanceof Durability)) { |
| 220 | + return false; |
| 221 | + } |
| 222 | + Durability other = (Durability) obj; |
| 223 | + if (!masterSync.equals(other.masterSync)) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + if (!replicaAck.equals(other.replicaAck)) { |
| 227 | + return false; |
| 228 | + } |
| 229 | + if (!replicaSync.equals(other.replicaSync)) { |
| 230 | + return false; |
| 231 | + } |
| 232 | + return true; |
| 233 | + } |
| 234 | +} |
0 commit comments