...

Source file src/discovery/db/db.go

Documentation: discovery/db

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  // Package db implements the storage solution for the service
    20  package db
    21  
    22  import (
    23  	"discovery/config"
    24  	"discovery/log"
    25  	"discovery/types"
    26  	"net"
    27  	"time"
    28  )
    29  
    30  const DatabasePath = "db"
    31  const DatabaseName = "p2pfaas-discovery.db"
    32  
    33  func init() {
    34  	log.Log.Debugf("Starting DB module")
    35  
    36  	initBackend()
    37  
    38  	AddInitServers(config.GetInitServers())
    39  	log.Log.Debugf("Added init servers %v", config.GetInitServers())
    40  
    41  	log.Log.Debugf("Init successfully")
    42  }
    43  
    44  func Start() {
    45  
    46  }
    47  
    48  func AddInitServers(initServersArr []string) {
    49  	initServersValid := 0
    50  
    51  	for _, s := range initServersArr {
    52  		// parse the IP
    53  		ip := net.ParseIP(s)
    54  		if ip == nil {
    55  			continue
    56  		}
    57  
    58  		err := MachineAdd(&types.Machine{
    59  			IP:         s,
    60  			Alive:      true,
    61  			DeadPolls:  0,
    62  			LastUpdate: time.Now().Unix(),
    63  		}, true)
    64  
    65  		if err != nil {
    66  			log.Log.Errorf("Could not add %s as init server: %s", s, err.Error())
    67  		} else {
    68  			initServersValid++
    69  			log.Log.Debugf("Added " + s + " as init server")
    70  		}
    71  	}
    72  	log.Log.Infof("Init DB with %d init servers", initServersValid)
    73  }
    74  

View as plain text