-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from qq254963746/develop
抽象 FailStore 接口,后面添加其他实现, 后面计划,优化任务队列模型, 提交处理性能
- Loading branch information
Showing
20 changed files
with
305 additions
and
98 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
26 changes: 26 additions & 0 deletions
26
job-core/src/main/java/com/lts/job/core/failstore/FailStore.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,26 @@ | ||
package com.lts.job.core.failstore; | ||
|
||
import com.lts.job.core.domain.KVPair; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by hugui on 5/21/15. | ||
*/ | ||
public interface FailStore { | ||
|
||
public void open() throws FailStoreException; | ||
|
||
public void put(String key, Object value) throws FailStoreException; | ||
|
||
public void delete(String key) throws FailStoreException; | ||
|
||
public void delete(List<String> keys) throws FailStoreException; | ||
|
||
public <T> List<KVPair<String, T>> fetchTop(int size, Type type) throws FailStoreException; | ||
|
||
public void close() throws FailStoreException; | ||
|
||
public void destroy() throws FailStoreException; | ||
} |
23 changes: 23 additions & 0 deletions
23
job-core/src/main/java/com/lts/job/core/failstore/FailStoreException.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,23 @@ | ||
package com.lts.job.core.failstore; | ||
|
||
/** | ||
* Created by hugui on 5/21/15. | ||
*/ | ||
public class FailStoreException extends Exception { | ||
|
||
public FailStoreException(String message) { | ||
super(message); | ||
} | ||
|
||
public FailStoreException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public FailStoreException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
protected FailStoreException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
job-core/src/main/java/com/lts/job/core/failstore/FailStoreFactory.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,16 @@ | ||
package com.lts.job.core.failstore; | ||
|
||
import com.lts.job.core.cluster.Config; | ||
import com.lts.job.core.extension.Adaptive; | ||
import com.lts.job.core.extension.SPI; | ||
|
||
/** | ||
* Created by hugui on 5/21/15. | ||
*/ | ||
@SPI("leveldb") | ||
public interface FailStoreFactory { | ||
|
||
@Adaptive("job.fail.store") | ||
public FailStore getFailStore(Config config); | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
job-core/src/main/java/com/lts/job/core/failstore/leveldb/LeveldbFailStore.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,117 @@ | ||
package com.lts.job.core.failstore.leveldb; | ||
|
||
import com.lts.job.core.cluster.Config; | ||
import com.lts.job.core.domain.KVPair; | ||
import com.lts.job.core.failstore.FailStore; | ||
import com.lts.job.core.failstore.FailStoreException; | ||
import com.lts.job.core.file.FileAccessor; | ||
import com.lts.job.core.file.FileException; | ||
import com.lts.job.core.file.FileUtils; | ||
import com.lts.job.core.util.JSONUtils; | ||
import org.fusesource.leveldbjni.JniDBFactory; | ||
import org.iq80.leveldb.DB; | ||
import org.iq80.leveldb.DBIterator; | ||
import org.iq80.leveldb.Options; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by hugui on 5/21/15. | ||
*/ | ||
public class LeveldbFailStore implements FailStore { | ||
|
||
// 文件锁 (同一时间只能有一个线程在 检查提交失败的任务) | ||
private FileAccessor dbLock; | ||
/** | ||
* 数据库目录 | ||
*/ | ||
private File dbPath; | ||
|
||
private DB db; | ||
|
||
private Options options; | ||
|
||
public LeveldbFailStore(Config config) { | ||
dbPath = FileUtils.createDirIfNotExist(config.getFailStorePath()); | ||
options = new Options(); | ||
try { | ||
dbLock = new FileAccessor(config.getFailStorePath() + "___db.lock"); | ||
dbLock.createIfNotExist(); | ||
} catch (FileException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void open() throws FailStoreException { | ||
dbLock.tryLock(); | ||
try { | ||
db = JniDBFactory.factory.open(dbPath, options); | ||
} catch (IOException e) { | ||
throw new FailStoreException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void put(String key, Object value) throws FailStoreException { | ||
String valueString = JSONUtils.toJSONString(value); | ||
db.put(key.getBytes(), valueString.getBytes()); | ||
} | ||
|
||
@Override | ||
public void delete(String key) throws FailStoreException { | ||
if (key == null) { | ||
return; | ||
} | ||
db.delete(key.getBytes()); | ||
} | ||
|
||
@Override | ||
public void delete(List<String> keys) throws FailStoreException { | ||
if (keys == null || keys.size() == 0) { | ||
return; | ||
} | ||
for (String key : keys) { | ||
delete(key); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> List<KVPair<String, T>> fetchTop(int size, Type type) { | ||
List<KVPair<String, T>> list = new ArrayList<KVPair<String, T>>(size); | ||
DBIterator iterator = db.iterator(); | ||
for (iterator.seekToLast(); iterator.hasPrev(); iterator.prev()) { | ||
String key = new String(iterator.peekPrev().getKey()); | ||
T value = JSONUtils.parse(new String(iterator.peekPrev().getValue()), type); | ||
KVPair<String, T> pair = new KVPair<String, T>(key, value); | ||
list.add(pair); | ||
if (list.size() >= size) { | ||
break; | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
@Override | ||
public void close() throws FailStoreException { | ||
try { | ||
db.close(); | ||
dbLock.unlock(); | ||
} catch (IOException e) { | ||
throw new FailStoreException(e); | ||
} | ||
} | ||
|
||
public void destroy() throws FailStoreException { | ||
try { | ||
JniDBFactory.factory.destroy(dbPath, options); | ||
dbLock.delete(); | ||
} catch (IOException e) { | ||
throw new FailStoreException(e); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
job-core/src/main/java/com/lts/job/core/failstore/leveldb/LeveldbFailStoreFactory.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,15 @@ | ||
package com.lts.job.core.failstore.leveldb; | ||
|
||
import com.lts.job.core.cluster.Config; | ||
import com.lts.job.core.failstore.FailStore; | ||
import com.lts.job.core.failstore.FailStoreFactory; | ||
|
||
/** | ||
* Created by hugui on 5/21/15. | ||
*/ | ||
public class LeveldbFailStoreFactory implements FailStoreFactory { | ||
@Override | ||
public FailStore getFailStore(Config config) { | ||
return new LeveldbFailStore(config); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package com.lts.job.core.support; | ||
|
||
import com.lts.job.core.domain.KVPair; | ||
import com.lts.job.core.file.FileAccessor; | ||
import com.lts.job.core.file.FileException; | ||
import com.lts.job.core.file.FileUtils; | ||
import com.lts.job.core.util.JSONUtils; | ||
import org.fusesource.leveldbjni.JniDBFactory; | ||
|
@@ -16,10 +18,13 @@ | |
|
||
/** | ||
* LevelDB 存储 | ||
* | ||
* @author Robert HG ([email protected]) on 3/6/15. | ||
*/ | ||
public class LevelDBStore { | ||
|
||
// 文件锁 (同一时间只能有一个线程在 检查提交失败的任务) | ||
private FileAccessor dbLock; | ||
/** | ||
* 数据库目录 | ||
*/ | ||
|
@@ -32,9 +37,16 @@ public class LevelDBStore { | |
public LevelDBStore(String path) { | ||
dbPath = FileUtils.createDirIfNotExist(path); | ||
options = new Options(); | ||
try { | ||
dbLock = new FileAccessor(path + "___db.lock"); | ||
dbLock.createIfNotExist(); | ||
} catch (FileException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void open() throws IOException { | ||
dbLock.tryLock(); | ||
db = JniDBFactory.factory.open(dbPath, options); | ||
} | ||
|
||
|
@@ -91,10 +103,12 @@ public <T> List<KVPair<String, T>> getList(int size, Type type) { | |
|
||
public void close() throws IOException { | ||
db.close(); | ||
dbLock.unlock(); | ||
} | ||
|
||
public void destroy() throws IOException { | ||
JniDBFactory.factory.destroy(dbPath, options); | ||
dbLock.delete(); | ||
} | ||
|
||
} |
Oops, something went wrong.