-
Notifications
You must be signed in to change notification settings - Fork 703
/
CarbonLockFactory.java
126 lines (115 loc) · 4.72 KB
/
CarbonLockFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.carbondata.core.locks;
import org.apache.carbondata.common.logging.LogService;
import org.apache.carbondata.common.logging.LogServiceFactory;
import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.metadata.AbsoluteTableIdentifier;
import org.apache.carbondata.core.util.CarbonProperties;
/**
* This class is a Lock factory class which is used to provide lock objects.
* Using this lock object client can request the lock and unlock.
*/
public class CarbonLockFactory {
/**
* Attribute for LOGGER
*/
private static final LogService LOGGER =
LogServiceFactory.getLogService(CarbonLockFactory.class.getName());
/**
* lockTypeConfigured to check if zookeeper feature is enabled or not for carbon.
*/
private static String lockTypeConfigured;
protected static String lockPath;
static {
CarbonLockFactory.getLockTypeConfigured();
getLockpath();
}
/**
* This method will determine the lock type.
*
* @param absoluteTableIdentifier
* @param lockFile
* @return
*/
public static ICarbonLock getCarbonLockObj(AbsoluteTableIdentifier absoluteTableIdentifier,
String lockFile) {
String tablePath;
if (lockPath.isEmpty()) {
tablePath = absoluteTableIdentifier.getTablePath();
} else {
tablePath = lockPath + CarbonCommonConstants.FILE_SEPARATOR + absoluteTableIdentifier
.getCarbonTableIdentifier().getTableId();
}
if (lockTypeConfigured.equals(CarbonCommonConstants.CARBON_LOCK_TYPE_ZOOKEEPER)) {
return new ZooKeeperLocking(tablePath, lockFile);
} else if (tablePath.startsWith(CarbonCommonConstants.S3A_PREFIX) || tablePath
.startsWith(CarbonCommonConstants.S3N_PREFIX) || tablePath
.startsWith(CarbonCommonConstants.S3_PREFIX) || lockTypeConfigured
.equals(CarbonCommonConstants.CARBON_LOCK_TYPE_MEMORY)) {
lockTypeConfigured = CarbonCommonConstants.CARBON_LOCK_TYPE_MEMORY;
return new MemoryLock(absoluteTableIdentifier.getCarbonTableIdentifier().getTableUniqueName(),
lockFile);
} else if (tablePath.startsWith(CarbonCommonConstants.HDFSURL_PREFIX)) {
lockTypeConfigured = CarbonCommonConstants.CARBON_LOCK_TYPE_HDFS;
return new HdfsFileLock(tablePath, lockFile);
} else {
lockTypeConfigured = CarbonCommonConstants.CARBON_LOCK_TYPE_LOCAL;
return new LocalFileLock(tablePath, lockFile);
}
}
/**
*
* @param locFileLocation
* @return carbon lock
*/
public static ICarbonLock getCarbonLockObj(String locFileLocation, String lockFile,
String tableId) {
String lockFileLocation;
if (lockPath.isEmpty()) {
lockFileLocation = locFileLocation;
} else {
lockFileLocation = lockPath + CarbonCommonConstants.FILE_SEPARATOR + tableId;
}
switch (lockTypeConfigured) {
case CarbonCommonConstants.CARBON_LOCK_TYPE_LOCAL:
return new LocalFileLock(lockFileLocation, lockFile);
case CarbonCommonConstants.CARBON_LOCK_TYPE_ZOOKEEPER:
return new ZooKeeperLocking(lockFileLocation, lockFile);
case CarbonCommonConstants.CARBON_LOCK_TYPE_HDFS:
return new HdfsFileLock(lockFileLocation, lockFile);
case CarbonCommonConstants.CARBON_LOCK_TYPE_MEMORY:
return new MemoryLock(tableId, lockFile);
default:
throw new UnsupportedOperationException("Not supported the lock type");
}
}
/**
* This method will set the zookeeper status whether zookeeper to be used for locking or not.
*/
private static void getLockTypeConfigured() {
lockTypeConfigured = CarbonProperties.getInstance()
.getProperty(CarbonCommonConstants.LOCK_TYPE, CarbonCommonConstants.LOCK_TYPE_DEFAULT)
.toUpperCase();
LOGGER.info("Configured lock type is: " + lockTypeConfigured);
}
private static void getLockpath() {
lockPath = CarbonProperties.getInstance()
.getProperty(CarbonCommonConstants.LOCK_PATH, "")
.toUpperCase();
}
}