Add proper help and several other cleanup

master
Harshavardhana 10 years ago
parent b153834d4d
commit bcc4449de7
  1. 138
      main.go
  2. 10
      pkg/storage/drivers/memory/memory.go

@ -20,21 +20,25 @@ import (
"fmt" "fmt"
"os" "os"
"os/user" "os/user"
"path"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"errors" "errors"
"reflect"
"github.com/dustin/go-humanize"
"github.com/minio-io/cli" "github.com/minio-io/cli"
"github.com/minio-io/minio/pkg/api" "github.com/minio-io/minio/pkg/api"
"github.com/minio-io/minio/pkg/api/web" "github.com/minio-io/minio/pkg/api/web"
"github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/server" "github.com/minio-io/minio/pkg/server"
"github.com/minio-io/minio/pkg/server/httpserver" "github.com/minio-io/minio/pkg/server/httpserver"
"github.com/minio-io/minio/pkg/storage/drivers/donut"
"github.com/minio-io/minio/pkg/storage/drivers/memory" "github.com/minio-io/minio/pkg/storage/drivers/memory"
"github.com/minio-io/minio/pkg/utils/log" "github.com/minio-io/minio/pkg/utils/log"
"reflect"
) )
var globalDebugFlag = false var globalDebugFlag = false
@ -51,30 +55,50 @@ var modeCommands = []cli.Command{
var modeCmd = cli.Command{ var modeCmd = cli.Command{
Name: "mode", Name: "mode",
Subcommands: modeCommands, Subcommands: modeCommands,
Description: "Mode of execution",
} }
var memoryCmd = cli.Command{ var memoryCmd = cli.Command{
Name: "memory", Name: "memory",
Action: runMemory, Description: "Limit maximum memory usage to SIZE in [B, KB, MB, GB]",
Flags: []cli.Flag{ Action: runMemory,
cli.StringFlag{ CustomHelpTemplate: `NAME:
Name: "max-memory", minio {{.Name}} - {{.Description}}
Value: "100M",
Usage: "", USAGE:
}, minio {{.Name}} SIZE
},
EXAMPLES:
1. Limit maximum memory usage to 64MB
$ minio {{.Name}} 64MB
2. Limit maximum memory usage to 4GB
$ minio {{.Name}} 4GB
`,
} }
var donutCmd = cli.Command{ var donutCmd = cli.Command{
Name: "donut", Name: "donut",
Action: runDonut, Description: "Specify a path to instantiate donut",
Flags: []cli.Flag{}, Action: runDonut,
CustomHelpTemplate: `NAME:
minio {{.Name}} - {{.Description}}
USAGE:
minio {{.Name}} PATH
EXAMPLES:
1. Use a regular disk to create donut
$ minio {{.Name}} /mnt/disk1
2. Use a lvm group to create donut
$ minio {{.Name}} /media/lvm/groups
`,
} }
type memoryFactory struct { type memoryFactory struct {
server.Config server.Config
maxMemory uint64
maxMemory int64
} }
func (f memoryFactory) getStartServerFunc() startServerFunc { func (f memoryFactory) getStartServerFunc() startServerFunc {
@ -85,7 +109,7 @@ func (f memoryFactory) getStartServerFunc() startServerFunc {
httpConfig.CertFile = f.CertFile httpConfig.CertFile = f.CertFile
httpConfig.KeyFile = f.KeyFile httpConfig.KeyFile = f.KeyFile
httpConfig.Websocket = false httpConfig.Websocket = false
_, _, driver := memory.Start(1024 * 1024 * 1024) _, _, driver := memory.Start(f.maxMemory)
ctrl, status, _ := httpserver.Start(api.HTTPHandler(f.Domain, driver), httpConfig) ctrl, status, _ := httpserver.Start(api.HTTPHandler(f.Domain, driver), httpConfig)
return ctrl, status return ctrl, status
} }
@ -110,11 +134,20 @@ func (f webFactory) getStartServerFunc() startServerFunc {
type donutFactory struct { type donutFactory struct {
server.Config server.Config
path string
} }
func (f donutFactory) getStartServerFunc() startServerFunc { func (f donutFactory) getStartServerFunc() startServerFunc {
return func() (chan<- string, <-chan error) { return func() (chan<- string, <-chan error) {
return nil, nil httpConfig := httpserver.Config{}
httpConfig.Address = f.Address
httpConfig.TLS = f.TLS
httpConfig.CertFile = f.CertFile
httpConfig.KeyFile = f.KeyFile
httpConfig.Websocket = false
_, _, driver := donut.Start(f.path)
ctrl, status, _ := httpserver.Start(api.HTTPHandler(f.Domain, driver), httpConfig)
return ctrl, status
} }
} }
@ -162,25 +195,14 @@ func init() {
type startServerFunc func() (chan<- string, <-chan error) type startServerFunc func() (chan<- string, <-chan error)
func runCmd(c *cli.Context) {
// default to memory driver, 1GB
apiServerConfig := getAPIServerConfig(c)
memoryDriver := memoryFactory{
Config: apiServerConfig,
maxMemory: 1024 * 1024 * 1024,
}
apiServer := memoryDriver.getStartServerFunc()
webServer := getWebServerConfigFunc(c)
servers := []startServerFunc{apiServer, webServer}
startMinio(servers)
}
func runMemory(c *cli.Context) { func runMemory(c *cli.Context) {
if len(c.Args()) < 1 {
cli.ShowCommandHelpAndExit(c, "memory", 1) // last argument is exit code
}
apiServerConfig := getAPIServerConfig(c) apiServerConfig := getAPIServerConfig(c)
// TODO max-memory should take human readable values maxMemory, err := humanize.ParseBytes(c.Args().First())
maxMemory, err := strconv.ParseInt(c.String("max-memory"), 10, 64)
if err != nil { if err != nil {
log.Println("max-memory not a numeric value") log.Fatalf("MaxMemory not a numeric value with reason: %s", err)
} }
memoryDriver := memoryFactory{ memoryDriver := memoryFactory{
Config: apiServerConfig, Config: apiServerConfig,
@ -193,9 +215,21 @@ func runMemory(c *cli.Context) {
} }
func runDonut(c *cli.Context) { func runDonut(c *cli.Context) {
u, err := user.Current()
if err != nil {
log.Fatalln(err)
}
if len(c.Args()) < 1 {
cli.ShowCommandHelpAndExit(c, "donut", 1) // last argument is exit code
}
p := c.Args().First()
if strings.TrimSpace(p) == "" {
p = path.Join(u.HomeDir, "minio-storage", "donut")
}
apiServerConfig := getAPIServerConfig(c) apiServerConfig := getAPIServerConfig(c)
donutDriver := donutFactory{ donutDriver := donutFactory{
Config: apiServerConfig, Config: apiServerConfig,
path: p,
} }
apiServer := donutDriver.getStartServerFunc() apiServer := donutDriver.getStartServerFunc()
webServer := getWebServerConfigFunc(c) webServer := getWebServerConfigFunc(c)
@ -207,12 +241,12 @@ func getAPIServerConfig(c *cli.Context) server.Config {
certFile := c.String("cert") certFile := c.String("cert")
keyFile := c.String("key") keyFile := c.String("key")
if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") { if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") {
log.Fatal("Both certificate and key must be provided to enable https") log.Fatalln("Both certificate and key must be provided to enable https")
} }
tls := (certFile != "" && keyFile != "") tls := (certFile != "" && keyFile != "")
return server.Config{ return server.Config{
Domain: c.String("domain"), Domain: c.GlobalString("domain"),
Address: c.String("api-address"), Address: c.GlobalString("api-address"),
TLS: tls, TLS: tls,
CertFile: certFile, CertFile: certFile,
KeyFile: keyFile, KeyFile: keyFile,
@ -221,8 +255,8 @@ func getAPIServerConfig(c *cli.Context) server.Config {
func getWebServerConfigFunc(c *cli.Context) startServerFunc { func getWebServerConfigFunc(c *cli.Context) startServerFunc {
config := server.Config{ config := server.Config{
Domain: c.String("domain"), Domain: c.GlobalString("domain"),
Address: c.String("web-address"), Address: c.GlobalString("web-address"),
TLS: false, TLS: false,
CertFile: "", CertFile: "",
KeyFile: "", KeyFile: "",
@ -282,24 +316,6 @@ func createSelectCases(channels []<-chan error) []reflect.SelectCase {
return cases return cases
} }
// Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B
func formatBytes(i int64) (result string) {
switch {
case i > (1024 * 1024 * 1024 * 1024):
result = fmt.Sprintf("%.02f TB", float64(i)/1024/1024/1024/1024)
case i > (1024 * 1024 * 1024):
result = fmt.Sprintf("%.02f GB", float64(i)/1024/1024/1024)
case i > (1024 * 1024):
result = fmt.Sprintf("%.02f MB", float64(i)/1024/1024)
case i > 1024:
result = fmt.Sprintf("%.02f KB", float64(i)/1024)
default:
result = fmt.Sprintf("%d B", i)
}
result = strings.Trim(result, " ")
return
}
// Tries to get os/arch/platform specific information // Tries to get os/arch/platform specific information
// Returns a map of current os/arch/platform/memstats // Returns a map of current os/arch/platform/memstats
func getSystemData() map[string]string { func getSystemData() map[string]string {
@ -310,10 +326,10 @@ func getSystemData() map[string]string {
memstats := &runtime.MemStats{} memstats := &runtime.MemStats{}
runtime.ReadMemStats(memstats) runtime.ReadMemStats(memstats)
mem := fmt.Sprintf("Used: %s | Allocated: %s | Used-Heap: %s | Allocated-Heap: %s", mem := fmt.Sprintf("Used: %s | Allocated: %s | Used-Heap: %s | Allocated-Heap: %s",
formatBytes(int64(memstats.Alloc)), humanize.Bytes(memstats.Alloc),
formatBytes(int64(memstats.TotalAlloc)), humanize.Bytes(memstats.TotalAlloc),
formatBytes(int64(memstats.HeapAlloc)), humanize.Bytes(memstats.HeapAlloc),
formatBytes(int64(memstats.HeapSys))) humanize.Bytes(memstats.HeapSys))
platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s", platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s",
host, host,
runtime.GOOS, runtime.GOOS,
@ -337,10 +353,8 @@ func main() {
app.Version = minioGitCommitHash app.Version = minioGitCommitHash
app.Author = "Minio.io" app.Author = "Minio.io"
app.Usage = "Minimalist Object Storage" app.Usage = "Minimalist Object Storage"
app.EnableBashCompletion = true
app.Flags = flags app.Flags = flags
app.Commands = commands app.Commands = commands
app.Action = runCmd
app.Before = func(c *cli.Context) error { app.Before = func(c *cli.Context) error {
globalDebugFlag = c.GlobalBool("debug") globalDebugFlag = c.GlobalBool("debug")
if globalDebugFlag { if globalDebugFlag {

@ -42,8 +42,8 @@ type memoryDriver struct {
objectMetadata map[string]storedObject objectMetadata map[string]storedObject
objects *lru.Cache objects *lru.Cache
lock *sync.RWMutex lock *sync.RWMutex
totalSize int64 totalSize uint64
maxSize int64 maxSize uint64
} }
type storedBucket struct { type storedBucket struct {
@ -57,7 +57,7 @@ type storedObject struct {
} }
// Start memory object server // Start memory object server
func Start(maxSize int64) (chan<- string, <-chan error, drivers.Driver) { func Start(maxSize uint64) (chan<- string, <-chan error, drivers.Driver) {
ctrlChannel := make(chan string) ctrlChannel := make(chan string)
errorChannel := make(chan error) errorChannel := make(chan error)
@ -199,7 +199,7 @@ func (memory *memoryDriver) CreateObject(bucket, key, contentType, md5sum string
} }
memory.objectMetadata[objectKey] = newObject memory.objectMetadata[objectKey] = newObject
memory.objects.Add(objectKey, dataSlice) memory.objects.Add(objectKey, dataSlice)
memory.totalSize = memory.totalSize + newObject.metadata.Size memory.totalSize = memory.totalSize + uint64(newObject.metadata.Size)
for memory.totalSize > memory.maxSize { for memory.totalSize > memory.maxSize {
memory.objects.RemoveOldest() memory.objects.RemoveOldest()
} }
@ -376,7 +376,7 @@ func (memory *memoryDriver) GetObjectMetadata(bucket, key, prefix string) (drive
func (memory *memoryDriver) evictObject(key lru.Key, value interface{}) { func (memory *memoryDriver) evictObject(key lru.Key, value interface{}) {
k := key.(string) k := key.(string)
memory.totalSize = memory.totalSize - memory.objectMetadata[k].metadata.Size memory.totalSize = memory.totalSize - uint64(memory.objectMetadata[k].metadata.Size)
log.Println("evicting:", k) log.Println("evicting:", k)
delete(memory.objectMetadata, k) delete(memory.objectMetadata, k)
} }

Loading…
Cancel
Save