@ThreadSafe public class LightIdBufferedProvider extends Object implements LightIdProvider
Lightweight lock, high performance, double-buffered light-id provider. The following 3 types of threads exist in total, and read threads are promoted to write threads and even load threads. At the same time, there are multiple read threads, but only unique write threads, and unique load threads. - Read thread, normal light-id consumer - Write thread, upgraded read thread or load thread to append fragment to buffer (segment) - Load thread, async thread or upgraded read thread to load segment via loader. Double buffer works as the following mechanism, it will track the id usage and auto control the count of preloading, but not exceed maxCount. - When the Id balance is less than 20%, the only async preload is `maxUsage in 60s` or `maxCount`. - When Id balance is exhausted, read threads upgrade to write threads, other read threads wait until woken up or timeout. - When read thread upgrades to write thread, loader exists, this read thread switches buffer after spinning busy and so on.
LightIdProvider.Generator, LightIdProvider.Loader, LightIdProvider.Segment| Modifier and Type | Field and Description |
|---|---|
static long |
ERR_ALIVE |
static int |
FIX_COUNT |
static LightIdProvider.Generator |
GENERATOR |
static int |
MAX_COUNT |
static int |
MAX_ERROR |
static int |
MIN_COUNT |
static long |
TIME_OUT |
| Constructor and Description |
|---|
LightIdBufferedProvider(@NotNull LightIdProvider.Loader loader)
default thread pool is core-size=3, max-size=64, keep-alive 60S
|
LightIdBufferedProvider(@NotNull LightIdProvider.Loader loader,
@NotNull ExecutorService executor)
Buffered Multiple Thread Provider
|
| Modifier and Type | Method and Description |
|---|---|
void |
cleanError(@NotNull String name,
int block)
clean the error, and reset the counter.
|
long |
getErrAlive() |
int |
getFixCount() |
LightIdProvider.Generator |
getGenerator() |
int |
getMaxCount() |
int |
getMaxError() |
int |
getMinCount() |
long |
getTimeout() |
long |
next(@NotNull String name,
int block)
get the next LightId by its block and name, default timeout is 1 second.
|
long |
next(@NotNull String name,
int block,
long timeout)
get the next LightId by its block and name ,within the specify timeout(ms)
|
void |
preload(int block)
Preload all LightId's in the block, doing this once at startup is enough.
|
void |
setErrAlive(long t)
set the error status alive time, which will be cleared when it expires, default 2 minutes.
|
boolean |
setFixCount(int n)
Whether to preload a fixed count of ids.
|
void |
setGenerator(@NotNull LightIdProvider.Generator generator)
set Sequence Handler to edit the sequence before the LightId
|
boolean |
setMaxCount(int n)
set the max count of preload, default is 10000
|
boolean |
setMaxError(int n)
set the max tolerated errors in the loading, default 5.
|
boolean |
setMinCount(int n)
set the min count of preload, default is 100.
|
boolean |
setTimeout(long t)
set request timeout in mills, default 1 second.
|
public static final int MAX_COUNT
public static final int MIN_COUNT
public static final int FIX_COUNT
public static final int MAX_ERROR
public static final long ERR_ALIVE
public static final long TIME_OUT
public static final LightIdProvider.Generator GENERATOR
public LightIdBufferedProvider(@NotNull
@NotNull LightIdProvider.Loader loader)
loader - the loaderpublic LightIdBufferedProvider(@NotNull
@NotNull LightIdProvider.Loader loader,
@NotNull
@NotNull ExecutorService executor)
loader - the loaderexecutor - the executorpublic long getErrAlive()
public long getTimeout()
public int getMaxError()
public int getMaxCount()
public int getMinCount()
public int getFixCount()
public LightIdProvider.Generator getGenerator()
public void setErrAlive(long t)
t - time in millsERR_ALIVEpublic boolean setTimeout(long t)
t - time in millsTIME_OUTpublic boolean setMaxError(int n)
n - countMAX_ERRORpublic boolean setMaxCount(int n)
n - countMAX_COUNTpublic boolean setMinCount(int n)
n - countMIN_COUNTpublic boolean setFixCount(int n)
n - count, Fixed count if greater than 0FIX_COUNTpublic void setGenerator(@NotNull
@NotNull LightIdProvider.Generator generator)
LightIdUtil.toId(int, long),
GENERATORpublic void preload(int block)
block - Id's blockpublic void cleanError(@NotNull
@NotNull String name,
int block)
name - Id's nameblock - Id's blockpublic long next(@NotNull
@NotNull String name,
int block)
LightIdProvidernext in interface LightIdProvidername - Id nameblock - block aka. data center in db clusterpublic long next(@NotNull
@NotNull String name,
int block,
long timeout)
LightIdProvidernext in interface LightIdProvidername - Id nameblock - block aka. data center in db clustertimeout - timeout in ms, 0(unlimited) is NOT recommended, as much as possible to meet the business performance requirements.Copyright © 2023. All rights reserved.