...

Source file src/discovery/db/utils.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
    20  
    21  import (
    22  	"context"
    23  	"discovery/config"
    24  	"discovery/log"
    25  	"discovery/types"
    26  	"fmt"
    27  	"time"
    28  )
    29  
    30  // DeclarePollFailed declares the machine as dead when the threshold of dead polls is reached
    31  func DeclarePollFailed(machine *types.Machine) {
    32  	machine.DeadPolls++
    33  
    34  	// if machine already marked as not alive then delete from DB
    35  	if !machine.Alive {
    36  		log.Log.Warningf("Deleting machine %s since already dead", machine.IP)
    37  		return
    38  	}
    39  
    40  	// otherwise declar as not alive
    41  	if machine.DeadPolls >= config.GetMachineDeadPollsRemovingThreshold() {
    42  		machine.Alive = false
    43  	}
    44  	machine.LastUpdate = time.Now().Unix()
    45  
    46  	_, err := MachineUpdate(machine)
    47  	if err != nil {
    48  		log.Log.Warningf("Could not update the machine %s", machine.IP)
    49  	}
    50  	log.Log.Debugf("Poll for machine %s failed", machine.IP)
    51  }
    52  
    53  // DeclarePollSucceeded declare the machine as alive and reset the dead polls counter
    54  func DeclarePollSucceeded(machine *types.Machine, ping float64) {
    55  	machine.Ping = ping
    56  	machine.Alive = true
    57  	machine.DeadPolls = 0
    58  	machine.LastUpdate = time.Now().Unix()
    59  
    60  	_, err := MachineUpdate(machine)
    61  	if err != nil {
    62  		log.Log.Warningf("Could not update the machine %s", machine.IP)
    63  	}
    64  	log.Log.Debugf("Poll for machine %s succeeded", machine.IP)
    65  }
    66  
    67  /*
    68   * Core
    69   */
    70  
    71  func getDatabaseDirPath() string {
    72  	return config.GetDataPath() // + "/" + DatabasePath
    73  }
    74  
    75  func getDatabaseFilePath() string {
    76  	return getDatabaseDirPath() + "/" + DatabaseName
    77  }
    78  
    79  func executeTransaction(query string, args ...interface{}) error {
    80  	tx, err := db.Begin()
    81  	if err != nil {
    82  		return fmt.Errorf("cannot begin transaction: %s", err.Error())
    83  	}
    84  
    85  	stmt, err := db.Prepare(query)
    86  	if err != nil {
    87  		return fmt.Errorf("cannot prepare query: %s", err.Error())
    88  	}
    89  
    90  	_, err = stmt.ExecContext(context.Background(), query, args)
    91  	if err != nil {
    92  		return fmt.Errorf("cannot execute query: %s", err.Error())
    93  	}
    94  
    95  	err = tx.Commit()
    96  	if err != nil {
    97  		return fmt.Errorf("cannot commit query: %s", err.Error())
    98  	}
    99  
   100  	return nil
   101  }
   102  

View as plain text