New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初期化を全ノードに実行できる準備 #11

Merged
merged 12 commits into from Oct 20, 2018
@@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"path"
"log"
)
var (
@@ -71,6 +72,8 @@ func (b *Isubank) Check(bankID string, price int64) error {
"bank_id": bankID,
"price": price,
}
log.Printf("[DEBUG] isubank Check %s %#v", b.appID, b.endpoint)
if err := b.request("/check", v, res); err != nil {
return fmt.Errorf("check failed. err: %s", err)
}
@@ -1,6 +1,7 @@
package controller
import (
"bytes"
"context"
"database/sql"
"encoding/json"
@@ -37,6 +38,23 @@ func NewHandler(db *sql.DB, store sessions.Store) *Handler {
}
}
func callOtherInit(url string) error {
body := &bytes.Buffer{}
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return err
}
client := &http.Client{Timeout: time.Duration(3) * time.Second}
res, err := client.Do(req)
if err != nil {
log.Println("[ERROR] CallInternalInitialize %s %s", url, err)
return err
}
defer res.Body.Close()
return nil
}
func (h *Handler) Initialize(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
err := h.txScope(func(tx *sql.Tx) error {
if err := model.InitBenchmark(tx); err != nil {
@@ -54,6 +72,24 @@ func (h *Handler) Initialize(w http.ResponseWriter, r *http.Request, _ httproute
}
return nil
})
if err != nil {
h.handleError(w, err, 500)
} else {
ips := []string{"127.0.0.1", "172.16.15.2", "172.16.15.3", "172.16.15.4"}
for _, ip := range ips {
// ignore errors
callOtherInit("http://" + ip + ":5000/internalInitialize")
}
h.handleSuccess(w, struct{}{})
}
}
func (h *Handler) InternalInitialize(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
err := h.txScope(func(tx *sql.Tx) error {
return model.SyncSetting(tx)
})
if err != nil {
h.handleError(w, err, 500)
} else {
@@ -305,6 +341,7 @@ func (h *Handler) DeleteOrders(w http.ResponseWriter, r *http.Request, p httprou
func (h *Handler) CommonMiddleware(f http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// log.Println("CommonMiddleware called");
if r.Method == http.MethodPost {
if err := r.ParseForm(); err != nil {
h.handleError(w, err, 400)
@@ -4,8 +4,6 @@ import (
"isucon8/isubank"
"isucon8/isulogger"
"log"
"github.com/pkg/errors"
)
const (
@@ -21,41 +19,57 @@ type Setting struct {
Val string
}
type InternalSetting struct {
bankEndpoint string
bankAppid string
logEndpoint string
logAppid string
}
var (
internalSetting InternalSetting
)
func SetSetting(d QueryExecutor, k, v string) error {
_, err := d.Exec(`INSERT INTO setting (name, val) VALUES (?, ?) ON DUPLICATE KEY UPDATE val = VALUES(val)`, k, v)
return err
}
func GetSetting(d QueryExecutor, k string) (string, error) {
s, err := scanSetting(d.Query(`SELECT * FROM setting WHERE name = ?`, k))
func SyncSetting(d QueryExecutor) error {
s, err := scanSetting(d.Query(`SELECT * FROM setting WHERE name = "bank_endpoint"`))
if err != nil {
return "", err
return err
}
return s.Val, nil
}
internalSetting.bankEndpoint = s.Val
func Isubank(d QueryExecutor) (*isubank.Isubank, error) {
ep, err := GetSetting(d, BankEndpoint)
s, err = scanSetting(d.Query(`SELECT * FROM setting WHERE name = "bank_appid"`))
if err != nil {
return nil, errors.Wrapf(err, "getSetting failed. %s", BankEndpoint)
return err
}
id, err := GetSetting(d, BankAppid)
if err != nil {
return nil, errors.Wrapf(err, "getSetting failed. %s", BankAppid)
}
return isubank.NewIsubank(ep, id)
}
internalSetting.bankAppid = s.Val
func Logger(d QueryExecutor) (*isulogger.Isulogger, error) {
ep, err := GetSetting(d, LogEndpoint)
s, err = scanSetting(d.Query(`SELECT * FROM setting WHERE name = "log_endpoint"`))
if err != nil {
return nil, errors.Wrapf(err, "getSetting failed. %s", LogEndpoint)
return err
}
id, err := GetSetting(d, LogAppid)
internalSetting.logEndpoint = s.Val
s, err = scanSetting(d.Query(`SELECT * FROM setting WHERE name = "log_appid"`))
if err != nil {
return nil, errors.Wrapf(err, "getSetting failed. %s", LogAppid)
return err
}
return isulogger.NewIsulogger(ep, id)
internalSetting.logAppid = s.Val
log.Println("[DEBUG] SyncSetting %#v", internalSetting)
return nil
}
func Isubank(d QueryExecutor) (*isubank.Isubank, error) {
return isubank.NewIsubank(internalSetting.bankEndpoint, internalSetting.logAppid)
}
func Logger(d QueryExecutor) (*isulogger.Isulogger, error) {
return isulogger.NewIsulogger(internalSetting.logEndpoint, internalSetting.logAppid)
}
func sendLog(d QueryExecutor, tag string, v interface{}) {
@@ -39,8 +39,8 @@ func main() {
port = getEnv("APP_PORT", "5000")
dbhost = getEnv("DB_HOST", "127.0.0.1")
dbport = getEnv("DB_PORT", "13306")
dbuser = getEnv("DB_USER", "root")
dbpass = getEnv("DB_PASSWORD", "")
dbuser = getEnv("DB_USER", "isucon")
dbpass = getEnv("DB_PASSWORD", "isucon")
dbname = getEnv("DB_NAME", "isucoin")
public = getEnv("PUBLIC_DIR", "public")
)
@@ -61,6 +61,7 @@ func main() {
router := httprouter.New()
router.POST("/initialize", h.Initialize)
router.POST("/internalInitialize", h.InternalInitialize)
router.POST("/signup", h.Signup)
router.POST("/signin", h.Signin)
router.POST("/signout", h.Signout)
@@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"path"
"log"
"time"
)
@@ -60,6 +61,7 @@ func NewIsulogger(endpoint, appID string) (*Isulogger, error) {
return nil, err
}
}
isulogger.update(endpoint,appID)
return isulogger, nil
}
@@ -73,8 +75,8 @@ func (b *Isulogger) Loop() {
messages = append(messages, l)
case <-t.C:
if len(messages) > 0 {
fmt.Println("send_bulk", len(messages))
go b.request("/send_bulk", messages)
b.request("/send_bulk", messages)
log.Printf("[DEBUG] send_bulk %d", len(messages))
messages = make([]*Log, 0)
}
}
@@ -88,11 +90,23 @@ func (b *Isulogger) Send(tag string, data interface{}) error {
return nil
}
func (b *Isulogger) update(endpoint, appID string) {
u, err := url.Parse(endpoint)
if err != nil {
return
}
isulogger.endpoint = u
isulogger.appID = appID
}
func (b *Isulogger) request(p string, v interface{}) error {
u := new(url.URL)
*u = *b.endpoint
u.Path = path.Join(u.Path, p)
log.Println("[DEBUG] isulogger %s", u)
body := &bytes.Buffer{}
if err := json.NewEncoder(body).Encode(v); err != nil {
return fmt.Errorf("logger json encode failed. err: %s", err)
Copy path View file
@@ -0,0 +1,97 @@
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
Copy path View file
@@ -20,6 +20,7 @@ http {
"\tapptime:$upstream_response_time"
"\tvhost:$host";
include /etc/nginx/mime.types;
default_type application/octet-stream;
# error_log /dev/stdout error;
# access_log /dev/stdout ltsv;
ProTip! Use n and p to navigate between commits in a pull request.