Developing lightweight computation at the DSG edge

Commit 4db5f65f authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[goclidote] Improve management of errors



Use the glog package to manage errors, especially those involving communication
with the AntidoteDB go client.
Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent 6f5349e2
......@@ -7,6 +7,7 @@ import (
"uc-monitor-go-test/cnml"
"github.com/AntidoteDB/antidote-go-client"
"github.com/golang/glog"
)
// DatabaseHost defines the host of the AntidoteDB instance to connect to is running
......@@ -17,34 +18,50 @@ var DatabasePort = 8087
var api_host_port = "localhost:3000"
// Error levels
// FATAL output code
var FATAL = 1
// ERROR output code
var ERROR = 2
// WARNING output code
var WARNING = 3
// INFO output code
var INFO = 4
// AntidoteAddItemToSetInBucket save an item to a set in a bucket
func AntidoteAddItemToSetInBucket(bucketName string, setName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteAddItemToSetInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteAddItemToSetInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetAdd(key, []byte(itemValue)))
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteAddItemToSetInBucket]: Unable to add value %s to set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteAddItemToSetInBucket]: Unable to read set %s in bucket %s", setName, bucketName))
err = tx.Commit()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteAddItemToSetInBucket]: Unable to commit transaction")
// Return true if the value belongs now to the set (either it has been added now, or was there already)
for _, v := range setVal {
if string(v) == itemValue {
return true
}
}
// Return false otherwise
return false
}
......@@ -52,20 +69,20 @@ func AntidoteAddItemToSetInBucket(bucketName string, setName string, itemValue s
func AntidoteReadRegisterInBucket(bucketName string, registerName string) string {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteReadRegisterInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteReadRegisterInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
setVal, err := bucket.ReadReg(tx, key)
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteReadRegisterInBucket]: Unable to read register %s in bucket %s", registerName, bucketName))
err = tx.Commit()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteReadRegisterInBucket]: Unable to commit transaction")
return string(setVal[:])
}
......@@ -74,28 +91,30 @@ func AntidoteReadRegisterInBucket(bucketName string, registerName string) string
func AntidoteSetRegisterInBucket(bucketName string, registerName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteSetRegisterInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteSetRegisterInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(itemValue)))
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteSetRegisterInBucket]: Unable to set value %s to register %s in bucket %s", itemValue, registerName, bucketName))
setVal, err := bucket.ReadReg(tx, key)
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteSetRegisterInBucket]: Unable to read register %s in bucket %s", registerName, bucketName))
err = tx.Commit()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteSetRegisterInBucket]: Unable to commit transaction")
// Return true if the register value is itemValue
if string(setVal[:]) == itemValue {
return true
}
// Return false otherwise
return false
}
......@@ -103,28 +122,30 @@ func AntidoteSetRegisterInBucket(bucketName string, registerName string, itemVal
func AntidoteRemoveRegisterInBucket(bucketName string, registerName string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteRemoveRegisterInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteRemoveRegisterInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(registerName)
err = bucket.Update(tx, antidoteclient.RegPut(key, []byte("")))
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteRemoveRegisterInBucket]: Unable to set a null value to register %s in bucket %s", registerName, bucketName))
setVal, err := bucket.ReadReg(tx, key)
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteRemoveRegisterInBucket]: Unable to read register %s in bucket %s", registerName, bucketName))
err = tx.Commit()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteRemoveRegisterInBucket]: Unable to commit transaction")
// Return true if the register value is null (0 length)
if len(setVal) == 0 {
return true
}
// Return false otherwise
return false
}
......@@ -134,20 +155,20 @@ func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []strin
var items []string
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteReadItemsFromSetInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteReadItemsFromSetInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteReadItemsFromSetInBucket]: Unable to read set %s in bucket %s", setName, bucketName))
err = tx.Commit()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteReadItemsFromSetInBucket]: Unable to commit transaction")
for _, v := range setVal {
items = append(items, string(v))
......@@ -160,42 +181,44 @@ func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []strin
func AntidoteRemoveItemFromSetInBucket(bucketName string, setName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteRemoveItemFromSetInBucket]: Unable to create a new AntidoteDB go client")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteRemoveItemFromSetInBucket]: Unable to start a transaction using the go client")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteRemoveItemFromSetInBucket]: Unable to remove item %s from set %s in bucket %s", itemValue, setName, bucketName))
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
errCheck(err, ERROR, fmt.Sprintf("[AntidoteRemoveItemFromSetInBucket]: Unable to read set %s in bucket %s", setName, bucketName))
err = tx.Commit()
errCheck(err, "")
errCheck(err, ERROR, "[AntidoteRemoveItemFromSetInBucket]: Unable to commit transaction")
itemNotInSet := true
// Return false if the item remains in the set
for _, v := range setVal {
if string(v[:]) == itemValue {
itemNotInSet = false
}
}
// Return true otherwise (even if it had never been there)
return itemNotInSet
}
// ReadDevicesFromAntidote read all the devices from Antidote using João's REST server
// ReadDevicesFromAntidote read all the devices from Antidote using the Go client
func ReadDevicesFromAntidote() []cnml.DeviceIpv4sGraphserver {
var devices []cnml.DeviceIpv4sGraphserver
for _, v := range AntidoteReadItemsFromSetInBucket("guifi", "devices") {
thisDevID, err := strconv.Atoi(v)
errCheck(err, "")
errCheck(err, WARNING, fmt.Sprintf("[ReadDevicesFromAntidote]: An error occurred when converting %s to int", v))
var thisDev cnml.DeviceIpv4sGraphserver
thisDev.ID = thisDevID
thisDev.GraphServer, err = strconv.Atoi(AntidoteReadRegisterInBucket(fmt.Sprintf("device-%d", thisDevID), "graphserver"))
......@@ -209,10 +232,22 @@ func ReadDevicesFromAntidote() []cnml.DeviceIpv4sGraphserver {
return devices
}
// Panic on error
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
// The errCheck function gracefully manages errors, warnings, etc.
func errCheck(e error, level int, message string) {
if message == "" {
message = "Unknown error"
}
if e != nil {
switch level {
case FATAL:
glog.Fatalln(message, e)
case ERROR:
glog.Errorln(message, e)
case WARNING:
glog.Warningln(message, e)
default:
glog.Infoln(message, e)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment