forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support redis lua for transaction storage & global lock (apa…
- Loading branch information
Showing
48 changed files
with
2,682 additions
and
472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
server/src/main/java/io/seata/server/storage/redis/LuaParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed 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 io.seata.server.storage.redis; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import io.seata.common.exception.StoreException; | ||
import io.seata.common.io.FileLoader; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.exceptions.JedisDataException; | ||
|
||
/** | ||
* lua related utils | ||
* | ||
* @author conghuhu | ||
*/ | ||
public class LuaParser { | ||
|
||
private static final String WHITE_SPACE = " "; | ||
|
||
private static final String ANNOTATION_LUA = "--"; | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
public final static class LuaResult implements Serializable { | ||
private static final long serialVersionUID = -4160065043902060730L; | ||
private Boolean success; | ||
private String status; | ||
private String data; | ||
|
||
public Boolean getSuccess() { | ||
return success; | ||
} | ||
|
||
public void setSuccess(Boolean success) { | ||
this.success = success; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public void setData(String data) { | ||
this.data = data; | ||
} | ||
|
||
@Override public String toString() { | ||
return "LuaResult{" + | ||
"success=" + success + | ||
", type='" + status + '\'' + | ||
", data='" + data + '\'' + | ||
'}'; | ||
} | ||
} | ||
|
||
public final static class LuaErrorStatus { | ||
|
||
public static final String ANOTHER_ROLLBACKING = "AnotherRollbackIng"; | ||
|
||
public static final String ANOTHER_HOLDING = "AnotherHoldIng"; | ||
|
||
public static final String XID_NOT_EXISTED = "NotExisted"; | ||
|
||
public static final String ILLEGAL_CHANGE_STATUS = "ChangeStatusFail"; | ||
} | ||
|
||
/** | ||
* get lua string from lua file. | ||
* | ||
* @param fileName | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public static Map<String, String> getEvalShaMapFromFile(String fileName) throws IOException { | ||
File luaFile = FileLoader.load(fileName); | ||
if (luaFile == null) { | ||
throw new IOException("no lua file: " + fileName); | ||
} | ||
StringBuilder luaByFile = new StringBuilder(); | ||
try (FileInputStream fis = new FileInputStream(luaFile)) { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(fis)); | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
if (line.trim().startsWith(ANNOTATION_LUA)) { | ||
continue; | ||
} | ||
luaByFile.append(line); | ||
luaByFile.append(WHITE_SPACE); | ||
} | ||
} catch (IOException e) { | ||
throw new IOException(e); | ||
} | ||
Map<String, String> resultMap = new ConcurrentHashMap<>(1); | ||
try (Jedis jedis = JedisPooledFactory.getJedisInstance()) { | ||
resultMap.put(fileName, jedis.scriptLoad(luaByFile.toString())); | ||
return resultMap; | ||
} catch (UnsupportedOperationException | JedisDataException e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
public static <T> T getObjectFromJson(String json, Class<T> classz) { | ||
try { | ||
return OBJECT_MAPPER.readValue(json, classz); | ||
} catch (JsonProcessingException e) { | ||
throw new StoreException(e.getMessage()); | ||
} | ||
} | ||
|
||
public static <T> List<T> getListFromJson(String json, Class<T> classz) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
return objectMapper.readValue(json, new TypeReference<List<T>>() { | ||
}); | ||
} catch (JsonProcessingException e) { | ||
throw new StoreException(e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.