Skip to content
Snippets Groups Projects
Unverified Commit a8160e64 authored by fagongzi's avatar fagongzi Committed by GitHub
Browse files

Add service main (#4207)

parent e403fde8
No related branches found
No related tags found
No related merge requests found
// Copyright 2022 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/matrixorigin/matrixone/pkg/dnservice"
"github.com/matrixorigin/matrixone/pkg/logutil"
)
const (
cnServiceType = "CN"
dnServiceType = "DN"
logServiceType = "LOG"
standaloneServiceType = "STANDALONE"
)
var (
supportServiceTypes = map[string]any{
cnServiceType: cnServiceType,
dnServiceType: dnServiceType,
logServiceType: logServiceType,
standaloneServiceType: standaloneServiceType,
}
)
// Config mo-service configuration
type Config struct {
// Log log config
Log logutil.LogConfig `toml:"log"`
// ServiceType service type, select the corresponding configuration to start the
// service according to the service type. [CN|DN|Log|Standalone]
ServiceType string `toml:"service-type"`
// DN dn service config
DN dnservice.Config `toml:"dn"`
}
func parseConfigFromFile(file string) (*Config, error) {
if file == "" {
return nil, fmt.Errorf("toml config file not set")
}
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
cfg := &Config{}
if _, err = toml.Decode(string(data), cfg); err != nil {
return nil, err
}
if err := cfg.validate(); err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) validate() error {
if _, ok := supportServiceTypes[strings.ToUpper(c.ServiceType)]; !ok {
return fmt.Errorf("service type %s not support", c.ServiceType)
}
return nil
}
// Copyright 2022 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/matrixorigin/matrixone/pkg/common/stopper"
"github.com/matrixorigin/matrixone/pkg/dnservice"
"github.com/matrixorigin/matrixone/pkg/logutil"
)
var (
uuid = flag.String("uuid", "", "UUID of the service node")
configFile = flag.String("cfg", "./mo.toml", "toml configuration used to start mo-service")
version = flag.Bool("version", false, "print version information")
)
func main() {
flag.Parse()
maybePrintVersion()
cfg, err := parseConfigFromFile(*configFile)
if err != nil {
panic(fmt.Sprintf("failed to parse config from %s, error: %s", *configFile, err.Error()))
}
setupLogger(cfg)
stopper := stopper.NewStopper("main", stopper.WithLogger(logutil.GetGlobalLogger()))
if err := startService(cfg, stopper); err != nil {
panic(err)
}
waitSignalToStop(stopper)
}
func setupLogger(cfg *Config) {
logutil.SetupMOLogger(&cfg.Log)
}
func waitSignalToStop(stopper *stopper.Stopper) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGINT)
<-sigchan
stopper.Stop()
}
func startService(cfg *Config, stopper *stopper.Stopper) error {
// TODO: start other service
switch strings.ToUpper(cfg.ServiceType) {
case dnServiceType:
return startDNService(cfg, stopper)
}
return nil
}
func startDNService(cfg *Config, stopper *stopper.Stopper) error {
if *uuid == "" {
return errors.New("UUID not set")
}
return stopper.RunNamedTask("dn-service", func(ctx context.Context) {
s, err := dnservice.NewService(&cfg.DN,
dnservice.WithLogger(logutil.GetGlobalLogger().Named("dn-service")))
if err != nil {
panic(err)
}
if err := s.Start(); err != nil {
panic(err)
}
<-ctx.Done()
if err := s.Close(); err != nil {
panic(err)
}
})
}
// Copyright 2022 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
)
var (
// GoVersion go version, setup by makefile
GoVersion = ""
// BranchName git branch, setup by makefile
BranchName = ""
// CommitID git commit id, setup by makefile
CommitID = ""
// BuildTime build time, setup by makefile
BuildTime = ""
// Version version, setup by makefile
Version = ""
)
func maybePrintVersion() {
if !*version {
return
}
fmt.Println("MatrixOne build info:")
fmt.Printf(" The golang version used to build this binary: %s\n", GoVersion)
fmt.Printf(" Git branch name: %s\n", BranchName)
fmt.Printf(" Git commit ID: %s\n", CommitID)
fmt.Printf(" Buildtime: %s\n", BuildTime)
fmt.Printf(" Version: %s\n", Version)
os.Exit(0)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment