@@ -90,9 +90,9 @@ public static <T extends AbstractId<T>> T getNextId(String name, ServerContext c
90
90
static final Lock tableNameLock = new ReentrantLock ();
91
91
static final Lock idLock = new ReentrantLock ();
92
92
93
- public static long reserveTable (Manager env , TableId tableId , long tid , boolean writeLock ,
93
+ public static long reserveTable (Manager env , TableId tableId , long tid , LockType lockType ,
94
94
boolean tableMustExist , TableOperation op ) throws Exception {
95
- if (getLock (env .getContext (), tableId , tid , writeLock ).tryLock ()) {
95
+ if (getLock (env .getContext (), tableId , tid , lockType ).tryLock ()) {
96
96
if (tableMustExist ) {
97
97
ZooReaderWriter zk = env .getContext ().getZooReaderWriter ();
98
98
if (!zk .exists (env .getContext ().getZooKeeperRoot () + Constants .ZTABLES + "/" + tableId )) {
@@ -101,29 +101,27 @@ public static long reserveTable(Manager env, TableId tableId, long tid, boolean
101
101
}
102
102
}
103
103
log .info ("table {} {} locked for {} operation: {}" , tableId , FateTxId .formatTid (tid ),
104
- ( writeLock ? "write" : "read" ) , op );
104
+ lockType , op );
105
105
return 0 ;
106
106
} else {
107
107
return 100 ;
108
108
}
109
109
}
110
110
111
- public static void unreserveTable (Manager env , TableId tableId , long tid , boolean writeLock ) {
112
- getLock (env .getContext (), tableId , tid , writeLock ).unlock ();
113
- log .info ("table {} {} unlocked for {}" , tableId , FateTxId .formatTid (tid ),
114
- (writeLock ? "write" : "read" ));
111
+ public static void unreserveTable (Manager env , TableId tableId , long tid , LockType lockType ) {
112
+ getLock (env .getContext (), tableId , tid , lockType ).unlock ();
113
+ log .info ("table {} {} unlocked for {}" , tableId , FateTxId .formatTid (tid ), lockType );
115
114
}
116
115
117
116
public static void unreserveNamespace (Manager env , NamespaceId namespaceId , long id ,
118
- boolean writeLock ) {
119
- getLock (env .getContext (), namespaceId , id , writeLock ).unlock ();
120
- log .info ("namespace {} {} unlocked for {}" , namespaceId , FateTxId .formatTid (id ),
121
- (writeLock ? "write" : "read" ));
117
+ LockType lockType ) {
118
+ getLock (env .getContext (), namespaceId , id , lockType ).unlock ();
119
+ log .info ("namespace {} {} unlocked for {}" , namespaceId , FateTxId .formatTid (id ), lockType );
122
120
}
123
121
124
122
public static long reserveNamespace (Manager env , NamespaceId namespaceId , long id ,
125
- boolean writeLock , boolean mustExist , TableOperation op ) throws Exception {
126
- if (getLock (env .getContext (), namespaceId , id , writeLock ).tryLock ()) {
123
+ LockType lockType , boolean mustExist , TableOperation op ) throws Exception {
124
+ if (getLock (env .getContext (), namespaceId , id , lockType ).tryLock ()) {
127
125
if (mustExist ) {
128
126
ZooReaderWriter zk = env .getContext ().getZooReaderWriter ();
129
127
if (!zk .exists (
@@ -133,7 +131,7 @@ public static long reserveNamespace(Manager env, NamespaceId namespaceId, long i
133
131
}
134
132
}
135
133
log .info ("namespace {} {} locked for {} operation: {}" , namespaceId , FateTxId .formatTid (id ),
136
- ( writeLock ? "write" : "read" ) , op );
134
+ lockType , op );
137
135
return 0 ;
138
136
} else {
139
137
return 100 ;
@@ -163,27 +161,30 @@ public static void unreserveHdfsDirectory(Manager env, String directory, long ti
163
161
}
164
162
165
163
private static Lock getLock (ServerContext context , AbstractId <?> id , long tid ,
166
- boolean writeLock ) {
164
+ LockType lockType ) {
167
165
byte [] lockData = FastFormat .toZeroPaddedHex (tid );
168
166
var fLockPath =
169
167
FateLock .path (context .getZooKeeperRoot () + Constants .ZTABLE_LOCKS + "/" + id .canonical ());
170
168
FateLock qlock = new FateLock (context .getZooReaderWriter (), fLockPath );
171
169
DistributedLock lock = DistributedReadWriteLock .recoverLock (qlock , lockData );
172
170
if (lock != null ) {
173
-
174
171
// Validate the recovered lock type
175
- boolean isWriteLock = lock .getType () == LockType .WRITE ;
176
- if (writeLock != isWriteLock ) {
172
+ if (lock .getType () != lockType ) {
177
173
throw new IllegalStateException ("Unexpected lock type " + lock .getType ()
178
174
+ " recovered for transaction " + FateTxId .formatTid (tid ) + " on object " + id
179
- + ". Expected " + ( writeLock ? LockType . WRITE : LockType . READ ) + " lock instead." );
175
+ + ". Expected " + lockType + " lock instead." );
180
176
}
181
177
} else {
182
178
DistributedReadWriteLock locker = new DistributedReadWriteLock (qlock , lockData );
183
- if (writeLock ) {
184
- lock = locker .writeLock ();
185
- } else {
186
- lock = locker .readLock ();
179
+ switch (lockType ) {
180
+ case WRITE :
181
+ lock = locker .writeLock ();
182
+ break ;
183
+ case READ :
184
+ lock = locker .readLock ();
185
+ break ;
186
+ default :
187
+ throw new IllegalStateException ("Unexpected LockType: " + lockType );
187
188
}
188
189
}
189
190
return lock ;
@@ -198,7 +199,7 @@ public static Lock getTableNameLock() {
198
199
}
199
200
200
201
public static Lock getReadLock (Manager env , AbstractId <?> id , long tid ) {
201
- return Utils .getLock (env .getContext (), id , tid , false );
202
+ return Utils .getLock (env .getContext (), id , tid , LockType . READ );
202
203
}
203
204
204
205
public static void checkNamespaceDoesNotExist (ServerContext context , String namespace ,
0 commit comments