Skip to content

Commit

Permalink
bug fix in strlock and other cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
cktan committed Oct 28, 2019
1 parent 66b0729 commit 174a026
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 74 deletions.
10 changes: 4 additions & 6 deletions src/s3pool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ func checkdirs() {
os.Exit(1)
}
}

mkdirall("log")
mkdirall("tmp")
mkdirall("data")
}


// Callback function for each new request
func serve(c *tcp_server.Client, request string) {

Expand All @@ -64,7 +63,7 @@ func serve(c *tcp_server.Client, request string) {
// log the request/response
log.Printf("%s [%s, %d bytes, %d ms]\n", request, status, len(reply), elapsed/1000)
}

startTime := time.Now()
var reply string
var err error
Expand Down Expand Up @@ -150,10 +149,9 @@ func boot() {
log.Println(os.Args)
}


func main() {
boot()
// boot()

// make sure that the aws cli is installed
if !checkawscli() {
exit("Cannot launch 'aws' command. Please install aws cli.")
Expand Down
1 change: 0 additions & 1 deletion src/s3pool/mon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"syscall"
)


func redirectFd() {
syscall.Close(0)
syscall.Close(1)
Expand Down
67 changes: 0 additions & 67 deletions src/s3pool/op/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,11 @@
package op

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)

func s3ListObjects_old(bucket string) error {

// write to a temp file and then move it into bucket/__list__
fp, err := ioutil.TempFile("tmp", "s3l_")
if err != nil {
return fmt.Errorf("Cannot create temp file -- %v", err)
}
defer fp.Close()
defer os.Remove(fp.Name())

// invoke s3api to list objects
cmd := exec.Command("aws", "s3api", "list-objects-v2",
"--bucket", bucket,
"--query", "Contents[].{Key: Key}")

pipe, _ := cmd.StdoutPipe()
if err = cmd.Start(); err != nil {
return fmt.Errorf("aws s3api list-objects failed -- %v", err)
}
defer cmd.Wait()

// read stdout of cmd
scanner := bufio.NewScanner(pipe)
for scanner.Scan() {
s := scanner.Text()
// Parse s of the form
// "Key" : "key value"
nv := strings.SplitN(s, ":", 2)
if len(nv) != 2 {
continue
}
name := strings.Trim(nv[0], " \t\"")
if name != "Key" {
continue
}
value := strings.Trim(nv[1], " \t\"")
// ignore empty value or value that looks like a DIR (ending with / )
if len(value) == 0 || value[len(value)-1] == '/' {
continue
}
fp.WriteString(value)
fp.WriteString("\n")
}
if err = scanner.Err(); err != nil {
return fmt.Errorf("aws s3api list-objects failed -- %v", err)
}

// done writing to temp file
fp.Close()

// clean up
if err = cmd.Wait(); err != nil {
return fmt.Errorf("aws s3api list-objects failed -- %v", err)
}

// move the temp file to data/bucket/__list__
if err = moveFile(fp.Name(), fmt.Sprintf("data/%s/__list__", bucket)); err != nil {
return err
}

return nil
}

func Refresh(args []string) (string, error) {
if len(args) != 1 {
return "", errors.New("expects 1 argument for REFRESH")
Expand Down
1 change: 1 addition & 0 deletions src/s3pool/op/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var trace_s3api bool = false
var use_goapi bool = false

func s3ListObjects(bucket string, wr io.Writer) error {
if trace_s3api {
Expand Down
23 changes: 23 additions & 0 deletions src/s3pool/strlock/strlock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package strlock

import (
"fmt"
"sync"
"time"
)

var tabmux sync.Mutex
Expand All @@ -26,4 +28,25 @@ func Unlock(s *string) {
tabmux.Lock()
defer tabmux.Unlock()
delete(tab, *s)
tabcond.Broadcast()
}

func Test() {
f := func(id int) {
key, _ := Lock("abcd")
time.Sleep(time.Second * 2)
fmt.Println(id, " up")
Unlock(key)
}
for i := 1; i < 10; i++ {
go f(i)
}

t := time.NewTicker(time.Second)
for i := 1; i < 100; i++ {
select {
case <-t.C:
fmt.Printf("tick %d\n", i)
}
}
}

0 comments on commit 174a026

Please sign in to comment.