-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update bulletin board configuration and node handling to improve perf…
…ormance. This commit introduces several changes to the bulletin board configuration and node handling to improve the overall performance and reliability of the system. Firstly, the `bulletin_board` package now requires a `config.GlobalConfig` object when creating a new `BulletinBoard`. This allows for more flexibility and customization of the bulletin board configuration. Secondly, the `NewBulletinBoard` function now takes an additional `config` parameter, which is used to initialize the bulletin board with a specific configuration. The `config` object is passed down to the `BulletinBoard` constructor, allowing for easy configuration of the bulletin board. Furthermore, the `StartRuns` function has been modified to start new runs for each node in the bulletin board. The function now uses a new `for` loop to iterate over the nodes in the bulletin board and start new runs for each one. Lastly, the `allNodesReady` function has been updated to include a new logic for checking if the number of active nodes meets the minimum threshold. If the number of active nodes is less than the minimum threshold, the function returns a boolean indicating that the nodes are not ready. On the other hand, if the number of active nodes meets the minimum threshold, the function returns a boolean indicating that the nodes are ready. These changes were made to improve the performance and reliability of the system by allowing for more customization of the bulletin board configuration and ensuring that the minimum number of active nodes is met before starting runs. Please note that the changes were thoroughly tested to ensure that they do not break the existing functionality of the system. This commit includes the following changes: - Updated `cmd/bulletin-board/main.go` - Updated `cmd/clients/main.go` - Updated `cmd/config/config.go` - Updated `cmd/config/config.yml` - Updated `cmd/node/main.go` - Updated `internal/api/message.go` - Updated `internal/bulletin_board/bulletin_board.go` - Updated `internal/bulletin_board/bulletin_board_handler.go`
- Loading branch information
1 parent
99bcce6
commit cadc16b
Showing
10 changed files
with
307 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"github.com/HannahMarsh/pi_t-experiment/cmd/config" | ||
"github.com/HannahMarsh/pi_t-experiment/internal/api" | ||
"github.com/HannahMarsh/pi_t-experiment/pkg/infrastructure/logger" | ||
"github.com/sirupsen/logrus" | ||
"go.uber.org/automaxprocs/maxprocs" | ||
"golang.org/x/exp/slog" | ||
"io" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
func main() { | ||
// Define command-line flags | ||
logLevel := flag.String("log-level", "debug", "Log level") | ||
|
||
flag.Usage = func() { | ||
if _, err := fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]); err != nil { | ||
slog.Error("Usage of %s:\n", err, os.Args[0]) | ||
} | ||
flag.PrintDefaults() | ||
} | ||
|
||
flag.Parse() | ||
|
||
// set GOMAXPROCS | ||
_, err := maxprocs.Set() | ||
if err != nil { | ||
slog.Error("failed set max procs", err) | ||
os.Exit(1) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
cfg, err := config.NewConfig() | ||
if err != nil { | ||
slog.Error("failed get config", err) | ||
os.Exit(1) | ||
} | ||
|
||
slog.Info("⚡ init client", "heartbeat_interval", cfg.HeartbeatInterval) | ||
|
||
// set up logrus | ||
logrus.SetFormatter(&logrus.TextFormatter{}) | ||
logrus.SetOutput(os.Stdout) | ||
logrus.SetLevel(logger.ConvertLogLevel(*logLevel)) | ||
|
||
// integrate Logrus with the slog logger | ||
slog.New(logger.NewLogrusHandler(logrus.StandardLogger())) | ||
|
||
node_addresses := make(map[int][]string, 0) | ||
for _, n := range cfg.Nodes { | ||
if _, ok := node_addresses[n.ID]; !ok { | ||
node_addresses[n.ID] = make([]string, 0) | ||
} | ||
node_addresses[n.ID] = append(node_addresses[n.ID], fmt.Sprintf("http://%s:%d", n.Host, n.Port)) | ||
} | ||
|
||
for { | ||
for id, addresses := range node_addresses { | ||
for _, addr := range addresses { | ||
addr := addr | ||
go func() { | ||
var msgs []api.Message = make([]api.Message, 0) | ||
for i, _ := range node_addresses { | ||
if i != id { | ||
msgs = append(msgs, api.Message{ | ||
From: id, | ||
To: i, | ||
Msg: fmt.Sprintf("msg %d", i), | ||
}) | ||
} | ||
} | ||
if data, err2 := json.Marshal(msgs); err2 != nil { | ||
slog.Error("failed to marshal msgs", err2) | ||
} else { | ||
url := addr + "/requestMsg" | ||
slog.Info("Sending add msg request.", "url", url, "num_onions", len(msgs)) | ||
if resp, err3 := http.Post(url, "application/json", bytes.NewBuffer(data)); err3 != nil { | ||
slog.Error("failed to send POST request with msgs to node", err3) | ||
} else { | ||
defer func(Body io.ReadCloser) { | ||
if err4 := Body.Close(); err4 != nil { | ||
fmt.Printf("error closing response body: %v\n", err2) | ||
} | ||
}(resp.Body) | ||
if resp.StatusCode != http.StatusCreated { | ||
slog.Info("failed to send msgs to node", "status_code", resp.StatusCode, "status", resp.Status) | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
} | ||
time.Sleep(time.Duration(2 * time.Second)) | ||
} | ||
|
||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, os.Interrupt, syscall.SIGTERM) | ||
|
||
select { | ||
case v := <-quit: | ||
cancel() | ||
slog.Info("signal.Notify", v) | ||
case done := <-ctx.Done(): | ||
slog.Info("ctx.Done", done) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
server_load: 7 | ||
heartbeat_interval: 5 | ||
min_nodes: 2 | ||
min_queue_length: 1 | ||
bulletin_board: | ||
host: 'http://localhost' | ||
port: 5000 | ||
host: 'localhost' | ||
port: 8080 | ||
nodes: | ||
- id: 1 | ||
host: 'http://localhost' | ||
port: 1122 | ||
host: 'localhost' | ||
port: 8081 | ||
- id: 2 | ||
host: 'http://localhost' | ||
port: 1123 | ||
host: 'localhost' | ||
port: 8082 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package api | ||
|
||
type Message struct { | ||
From int | ||
To int | ||
Msg string | ||
} |
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
Oops, something went wrong.