forked from stuarthu/go-implement-your-object-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I765d6ddf10200bcf3543e11f501b33f825a6844f Signed-off-by: Stuart <[email protected]>
- Loading branch information
Stuart
authored and
Stuart
committed
Jul 11, 2017
1 parent
060424a
commit 8940170
Showing
43 changed files
with
381 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../chapter2/lib/objectstream/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package locate | ||
|
||
import ( | ||
"../../lib/rabbitmq" | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type locateMessage struct { | ||
Addr string | ||
Id int | ||
} | ||
|
||
func Locate(name string) (locateInfo []locateMessage) { | ||
q := rabbitmq.New(os.Getenv("RABBITMQ_SERVER")) | ||
q.Publish("dataServers", name) | ||
c := q.Consume() | ||
go func() { | ||
time.Sleep(time.Second) | ||
q.Close() | ||
}() | ||
msg := <-c | ||
if len(msg.Body) == 0 { | ||
return | ||
} | ||
var info locateMessage | ||
json.Unmarshal(msg.Body, &info) | ||
locateInfo = append(locateInfo, info) | ||
return | ||
} | ||
|
||
func Handler(w http.ResponseWriter, r *http.Request) { | ||
m := r.Method | ||
if m != http.MethodGet { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
info := Locate(strings.Split(r.URL.EscapedPath(), "/")[2]) | ||
if len(info) == 0 { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
for i := range info { | ||
b, _ := json.Marshal(info[i]) | ||
w.Write(b) | ||
w.Write([]byte("\n")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,69 @@ | ||
package objects | ||
|
||
import ( | ||
"../../lib/objectstream" | ||
"../heartbeat" | ||
"../locate" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
func put(w http.ResponseWriter, r *http.Request) { | ||
hash := getHashFromHeader(r) | ||
if hash == "" { | ||
log.Println("missing object hash in digest header") | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
s := locate.Locate(hash) | ||
if s == "" { | ||
e := storeObject(r, hash) | ||
info := locate.Locate(url.PathEscape(hash)) | ||
if len(info) == 0 { | ||
c, e := storeObject(r) | ||
if e != nil { | ||
log.Println(e) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.WriteHeader(c) | ||
return | ||
} | ||
if c != http.StatusOK { | ||
w.WriteHeader(c) | ||
return | ||
} | ||
|
||
} | ||
|
||
e := addVersion(r, hash) | ||
e := addVersion(r) | ||
if e != nil { | ||
log.Println(e) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func storeObject(r *http.Request) (int, error) { | ||
s := heartbeat.ChooseRandomDataServer() | ||
if s == "" { | ||
return http.StatusServiceUnavailable, fmt.Errorf("cannot find any dataServer") | ||
} | ||
hash := getHashFromHeader(r) | ||
size := getSizeFromHeader(r) | ||
h := sha256.New() | ||
reader := io.TeeReader(r.Body, h) | ||
stream, e := objectstream.NewTempStream(s, url.PathEscape(hash), size) | ||
if e != nil { | ||
return http.StatusInternalServerError, e | ||
} | ||
_, e = io.Copy(stream, reader) | ||
if e != nil { | ||
stream.Close(false) | ||
return http.StatusInternalServerError, e | ||
} | ||
digest := base64.StdEncoding.EncodeToString(h.Sum(nil)) | ||
if digest != hash { | ||
stream.Close(false) | ||
return http.StatusBadRequest, fmt.Errorf("object hash mismatch, calculated=%s, requested=%s", digest, hash) | ||
} | ||
return stream.Close(true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.