Skip to content

Commit

Permalink
Replace logger with zap logger (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: Arrobo, Gabriel <[email protected]>
  • Loading branch information
gab-arrobo authored Oct 1, 2024
1 parent ae837cc commit c15c37a
Show file tree
Hide file tree
Showing 30 changed files with 326 additions and 829 deletions.
1 change: 0 additions & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ jobs:
install-go: false

lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1-dev
1.2.0
12 changes: 6 additions & 6 deletions drsm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type DrsmInterface interface {
}

func InitDRSM(sharedPoolName string, myid PodId, db DbInfo, opt *Options) (DrsmInterface, error) {
logger.AppLog.Debugln("CLIENT ID: ", myid)
logger.DrsmLog.Debugln("client id:", myid)

d := &Drsm{sharedPoolName: sharedPoolName,
clientId: myid,
Expand All @@ -66,7 +66,7 @@ func (d *Drsm) AllocateInt32ID() (int32, error) {
mutex.Lock()
defer mutex.Unlock()
if d.mode == ResourceDemux {
logger.AppLog.Debugf("demux mode can not allocate Resource index")
logger.DrsmLog.Debugln("demux mode can not allocate Resource index")
err := fmt.Errorf("demux mode does not allow Resource Id allocation")
return 0, err
}
Expand All @@ -87,7 +87,7 @@ func (d *Drsm) ReleaseInt32ID(id int32) error {
mutex.Lock()
defer mutex.Unlock()
if d.mode == ResourceDemux {
logger.AppLog.Debugf("demux mode can not release Resource index")
logger.DrsmLog.Debugln("demux mode can not release Resource index")
err := fmt.Errorf("demux mode does not allow Resource Id allocation")
return err
}
Expand All @@ -96,7 +96,7 @@ func (d *Drsm) ReleaseInt32ID(id int32) error {
chunk, found := d.localChunkTbl[chunkId]
if found {
chunk.ReleaseIntID(id)
logger.AppLog.Debugln("ID Released: ", id)
logger.DrsmLog.Debugln("id released:", id)
return nil
} else {
chunk, found := d.scanChunks[chunkId]
Expand Down Expand Up @@ -124,7 +124,7 @@ func (d *Drsm) FindOwnerInt32ID(id int32) (*PodId, error) {

func (d *Drsm) AcquireIp(pool string) (string, error) {
if d.mode == ResourceDemux {
logger.AppLog.Debugf("demux mode can not allocate Ip")
logger.DrsmLog.Debugln("demux mode can not allocate Ip")
err := fmt.Errorf("demux mode does not allow Resource allocation")
return "", err
}
Expand All @@ -133,7 +133,7 @@ func (d *Drsm) AcquireIp(pool string) (string, error) {

func (d *Drsm) ReleaseIp(pool, ip string) error {
if d.mode == ResourceDemux {
logger.AppLog.Debugf("demux mode can not Release Resource")
logger.DrsmLog.Debugln("demux mode can not Release Resource")
err := fmt.Errorf("demux mode does not allow Resource Release")
return err
}
Expand Down
15 changes: 7 additions & 8 deletions drsm/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package drsm

import (
"fmt"
"log"
"math/rand"
"strconv"
"strings"
Expand All @@ -25,7 +24,7 @@ func (d *Drsm) GetNewChunk() (*chunk, error) {
// We got to allocate new Chunk. We should select
// probable chunk number

logger.AppLog.Debugf("Allocate new chunk ")
logger.DrsmLog.Debugln("allocate new chunk")
// 14 bits --- 1,2,4,8,16
var cn int32 = 1
for {
Expand All @@ -37,7 +36,7 @@ func (d *Drsm) GetNewChunk() (*chunk, error) {
if found {
continue
}
logger.AppLog.Debugln("Found chunk Id block ", cn)
logger.DrsmLog.Debugln("found chunk Id block", cn)
break
}
// Let's confirm if this gets updated in DB
Expand All @@ -46,13 +45,13 @@ func (d *Drsm) GetNewChunk() (*chunk, error) {
update := bson.M{"_id": docId, "type": "chunk", "chunkId": docId, "podId": d.clientId.PodName, "podInstance": d.clientId.PodInstance, "podIp": d.clientId.PodIp}
inserted := d.mongo.RestfulAPIPostOnly(d.sharedPoolName, filter, update)
if !inserted {
log.Printf("Adding chunk %v failed. Retry again ", cn)
logger.DrsmLog.Errorf("Adding chunk %v failed. Retry again", cn)
continue
}
break
}

log.Printf("Adding chunk %v success ", cn)
logger.DrsmLog.Infof("Adding chunk %v success", cn)
c := &chunk{Id: cn}
c.AllocIds = make(map[int32]bool)
var i int32
Expand All @@ -69,7 +68,7 @@ func (d *Drsm) GetNewChunk() (*chunk, error) {

func (c *chunk) AllocateIntID() int32 {
if len(c.FreeIds) == 0 {
logger.AppLog.Debugf("FreeIds in chunk 0")
logger.DrsmLog.Debugln("freeIds in chunk 0")
return 0
}
id := c.FreeIds[len(c.FreeIds)-1]
Expand All @@ -81,7 +80,7 @@ func (c *chunk) ReleaseIntID(id int32) {
i := id & 0x3ff
for _, freeid := range c.FreeIds {
if freeid == i {
log.Printf("ID %v is already freed", freeid)
logger.DrsmLog.Warnf("id %v is already freed", freeid)
return
}
}
Expand All @@ -98,7 +97,7 @@ func (c *chunk) ReleaseIntID(id int32) {
}

func getChunIdFromDocId(id string) int32 {
log.Printf("id received: %v value", id)
logger.DrsmLog.Infof("id received: %v value", id)
z := strings.Split(id, "-")
if len(z) == 2 && z[0] == "chunkid" {
cid, _ := strconv.ParseInt(z[1], 10, 32)
Expand Down
12 changes: 6 additions & 6 deletions drsm/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
func (d *Drsm) podDownDetected() {
fmt.Println("started Pod Down goroutine")
for p := range d.podDown {
logger.AppLog.Infoln("pod Down detected", p)
logger.DrsmLog.Infoln("pod Down detected", p)
// Given Pod find out current Chunks owned by this POD
pd := d.podMap[p]
for k := range pd.podChunks {
d.globalChunkTblMutex.Lock()
c, found := d.globalChunkTbl[k]
d.globalChunkTblMutex.Unlock()
logger.AppLog.Debugf("found: %v chunk: %v", found, c)
logger.DrsmLog.Debugf("found: %v chunk: %v", found, c)
if found {
go c.claimChunk(d)
}
Expand All @@ -30,22 +30,22 @@ func (d *Drsm) podDownDetected() {

func (c *chunk) claimChunk(d *Drsm) {
if d.mode != ResourceClient {
logger.AppLog.Infof("claimChunk ignored demux mode ")
logger.DrsmLog.Infoln("claimChunk ignored demux mode")
return
}
// try to claim. If success then notification will update owner.
logger.AppLog.Debugf("claimChunk started")
logger.DrsmLog.Debugln("claimChunk started")
docId := fmt.Sprintf("chunkid-%d", c.Id)
update := bson.M{"_id": docId, "type": "chunk", "podId": d.clientId.PodName, "podInstance": d.clientId.PodInstance, "podIp": d.clientId.PodIp}
filter := bson.M{"_id": docId, "podId": c.Owner.PodName}
updated := d.mongo.RestfulAPIPutOnly(d.sharedPoolName, filter, update)
if updated == nil {
// TODO : don't add to local pool yet. We can add it only if scan is done.
logger.AppLog.Debugf("claimChunk success")
logger.DrsmLog.Debugln("claimChunk success")
c.Owner.PodName = d.clientId.PodName
c.Owner.PodIp = d.clientId.PodIp
go c.scanChunk(d)
} else {
logger.AppLog.Debugf("claimChunk failure ")
logger.DrsmLog.Debugln("claimChunk failure")
}
}
9 changes: 4 additions & 5 deletions drsm/drsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package drsm

import (
"log"
"sync"
"time"

Expand Down Expand Up @@ -64,13 +63,13 @@ type Drsm struct {
func (d *Drsm) DeletePod(podInstance string) {
filter := bson.M{"type": "keepalive", "podInstance": podInstance}
d.mongo.RestfulAPIDeleteMany(d.sharedPoolName, filter)
logger.AppLog.Infoln("Deleted PodId from DB: ", podInstance)
logger.DrsmLog.Infoln("deleted PodId from DB:", podInstance)
}

func (d *Drsm) ConstuctDrsm(opt *Options) {
if opt != nil {
d.mode = opt.Mode
logger.AppLog.Debugln("drsm mode set to ", d.mode)
logger.DrsmLog.Debugln("drsm mode set to", d.mode)
if opt.ResIdSize > 0 {
d.resIdSize = opt.ResIdSize
} else {
Expand All @@ -79,7 +78,7 @@ func (d *Drsm) ConstuctDrsm(opt *Options) {
d.resourceValidCb = opt.ResourceValidCb
}
d.chunkIdRange = 1 << (d.resIdSize - 10)
log.Printf("ChunkId in the range of 0 to %v ", d.chunkIdRange)
logger.DrsmLog.Debugf("chunkId in the range of 0 to %v", d.chunkIdRange)
d.localChunkTbl = make(map[int32]*chunk)
d.globalChunkTbl = make(map[int32]*chunk)
d.podMap = make(map[string]*podData)
Expand All @@ -90,7 +89,7 @@ func (d *Drsm) ConstuctDrsm(opt *Options) {

//connect to DB
d.mongo, _ = MongoDBLibrary.NewMongoClient(d.db.Url, d.db.Name)
logger.AppLog.Debugln("MongoClient is created.", d.db.Name)
logger.DrsmLog.Debugln("mongoClient is created", d.db.Name)

go d.handleDbUpdates()
go d.punchLiveness()
Expand Down
14 changes: 7 additions & 7 deletions drsm/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (
// TODO : should have ability to create new instances of ipam
func (d *Drsm) initIpam(opt *Options) {
if opt != nil {
logger.AppLog.Debugln("ipmodule ", opt)
logger.DrsmLog.Debugln("ipmodule", opt)
}
dbOptions := &options.ClientOptions{}
dbOptions = dbOptions.ApplyURI(d.db.Url)
dbConfig := ipam.MongoConfig{DatabaseName: d.db.Name, CollectionName: "ipaddress", MongoClientOptions: dbOptions}
mo, err := ipam.NewMongo(context.TODO(), dbConfig)
if err != nil {
logger.AppLog.Debugln("ipmodule error. NewMongo error ", err)
logger.DrsmLog.Debugf("ipmodule error. NewMongo error: %v", err)
}
ipModule := ipam.NewWithStorage(mo)
logger.AppLog.Debugln("ipmodule ", ipModule)
logger.DrsmLog.Debugln("ipmodule", ipModule)
d.ipModule = ipModule
d.prefix = make(map[string]*ipam.Prefix)

Expand All @@ -36,7 +36,7 @@ func (d *Drsm) initIpam(opt *Options) {
}
d.prefix[k] = prefix
}
logger.AppLog.Debugln("ip module prefix ", d.prefix)
logger.DrsmLog.Debugln("ip module prefix", d.prefix)
}

func (d *Drsm) initIpPool(name string, prefix string) error {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (d *Drsm) acquireIp(name string) (string, error) {
err := fmt.Errorf("no address")
return "", err
}
logger.AppLog.Debugln("Acquired IP ", ip.IP)
logger.DrsmLog.Debugln("acquired IP", ip.IP)
return ip.IP.String(), nil
}

Expand All @@ -83,10 +83,10 @@ func (d *Drsm) releaseIp(name, ip string) error {

err := d.ipModule.ReleaseIPFromPrefix(context.TODO(), prefix.Cidr, ip)
if err != nil {
logger.AppLog.Debugln("Release IP failed - ", ip)
logger.DrsmLog.Debugln("release IP failed - ", ip)
err := fmt.Errorf("no address")
return err
}
logger.AppLog.Debugln("Release IP successful ", ip)
logger.DrsmLog.Debugln("release IP successful", ip)
return nil
}
10 changes: 5 additions & 5 deletions drsm/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (

func (c *chunk) scanChunk(d *Drsm) {
if d.mode == ResourceDemux {
logger.AppLog.Infof("Don't perform scan task when demux mode is ON")
logger.DrsmLog.Infoln("do not perform scan task when demux mode is ON")
return
}

if c.Owner.PodName != d.clientId.PodName {
logger.AppLog.Infof("Don't perform scan task if Chunk is not owned by us")
logger.DrsmLog.Infoln("do not perform scan task if Chunk is not owned by us")
return
}
c.State = Scanning
Expand All @@ -31,7 +31,7 @@ func (c *chunk) scanChunk(d *Drsm) {
for {
select {
case <-ticker.C:
logger.AppLog.Debugf("let's scan one by one id for %v , chunk details %v ", c.Id, c)
logger.DrsmLog.Debugf("let's scan one by one id for %v, chunk details %v", c.Id, c)
// TODO : find candidate and then scan that Id.
// once all Ids are scanned then we can start using this block
if c.resourceValidCb != nil {
Expand All @@ -50,13 +50,13 @@ func (c *chunk) scanChunk(d *Drsm) {
c.State = Owned
d.localChunkTbl[c.Id] = c
delete(d.scanChunks, c.Id)
logger.AppLog.Debugf("scan complete for Chunk %v", c.Id)
logger.DrsmLog.Debugf("scan complete for Chunk %v", c.Id)
return
}
}
//no one is writing on stopScan for now. We will use it eventually
case <-c.stopScan:
logger.AppLog.Debugf("received Stop Scan. Closing scan for %v", c.Id)
logger.DrsmLog.Debugf("received Stop Scan. Closing scan for %v", c.Id)
return
}
}
Expand Down
Loading

0 comments on commit c15c37a

Please sign in to comment.