submit job from xcecli
This commit is contained in:
parent
c1c9f9e736
commit
d9b5c361c2
5 changed files with 148 additions and 281 deletions
|
|
@ -1,80 +0,0 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
/**
|
||||
* 31 bit timestamp(s) | | 5 bit clusterID | 10 bit sequenceBits
|
||||
*/
|
||||
const (
|
||||
clusterIDBits = uint64(5) // 5 bits clusterID
|
||||
sequenceBits = uint64(10)
|
||||
|
||||
maxClusterID = int64(-1) ^ (int64(-1) << clusterIDBits)
|
||||
maxSequence = int64(-1) ^ (int64(-1) << sequenceBits)
|
||||
|
||||
timeLeft = uint8(15) // timeLeft = clusterIDBits + sequenceBits //
|
||||
clusterLeft = uint8(10)
|
||||
|
||||
twepoch = int64(1735228800) // Dec 27 00:00:00 2024
|
||||
)
|
||||
|
||||
type Worker struct {
|
||||
mu sync.Mutex
|
||||
LastStamp int64 // last timestamp
|
||||
//WorkerID int64 //
|
||||
ClusterID int64 // cluster id
|
||||
Sequence int64
|
||||
}
|
||||
|
||||
// under distributed env, we need assign uniform id to each cluster: max 32 cluster.
|
||||
func NewWorker(clusterID int) *Worker {
|
||||
return &Worker{
|
||||
//WorkerID: workerID,
|
||||
LastStamp: 0,
|
||||
Sequence: 0,
|
||||
ClusterID: int64(clusterID),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) getSeconds() int64 {
|
||||
//return time.Now().UnixNano() / 1e9 // get timestamp in second.
|
||||
return time.Now().Unix() // / 1e9 // get timestamp in second.
|
||||
}
|
||||
|
||||
func (w *Worker) NextID() (int64, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
|
||||
return w.nextID()
|
||||
}
|
||||
|
||||
func (w *Worker) nextID() (int64, error) {
|
||||
timeStamp := w.getSeconds()
|
||||
if timeStamp < w.LastStamp {
|
||||
return 0, errors.New("time is moving backwards,waiting until")
|
||||
}
|
||||
|
||||
if w.LastStamp == timeStamp {
|
||||
w.Sequence = (w.Sequence + 1) & maxSequence
|
||||
|
||||
if w.Sequence == 0 {
|
||||
for timeStamp <= w.LastStamp {
|
||||
timeStamp = w.getSeconds()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.Sequence = 0
|
||||
}
|
||||
|
||||
w.LastStamp = timeStamp
|
||||
|
||||
id := ((timeStamp - twepoch) << timeLeft) |
|
||||
(w.ClusterID << clusterLeft) |
|
||||
w.Sequence
|
||||
|
||||
return int64(id), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue