X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6da9f3666bc0ddee2d0be079cc30ba8b82804706..3294cfcee6d9316df0d0fae9848118501f57d908:/services/api/app/models/node.rb diff --git a/services/api/app/models/node.rb b/services/api/app/models/node.rb index b57abfde91..3d8b91b4b6 100644 --- a/services/api/app/models/node.rb +++ b/services/api/app/models/node.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'tempfile' class Node < ArvadosModel @@ -102,25 +106,7 @@ class Node < ArvadosModel end end - # Assign slot_number - if self.slot_number.nil? - try_slot = 1 - begin - self.slot_number = try_slot - begin - self.save! - break - rescue ActiveRecord::RecordNotUnique - try_slot += 1 - end - raise "No available node slots" if try_slot == Rails.configuration.max_compute_nodes - end while true - end - - # Assign hostname - if self.hostname.nil? and Rails.configuration.assign_node_hostname - self.hostname = self.class.hostname_for_slot(self.slot_number) - end + assign_slot # Record other basic stats ['total_cpu_cores', 'total_ram_mb', 'total_scratch_mb'].each do |key| @@ -134,8 +120,45 @@ class Node < ArvadosModel save! end + def assign_slot + return if self.slot_number.andand > 0 + while true + self.slot_number = self.class.available_slot_number + if self.slot_number.nil? + raise "No available node slots" + end + begin + save! + return assign_hostname + rescue ActiveRecord::RecordNotUnique + # try again + end + end + end + protected + def assign_hostname + if self.hostname.nil? and Rails.configuration.assign_node_hostname + self.hostname = self.class.hostname_for_slot(self.slot_number) + end + end + + def self.available_slot_number + # Join the sequence 1..max with the nodes table. Return the first + # (i.e., smallest) value that doesn't match the slot_number of any + # existing node. + connection.exec_query('SELECT n FROM generate_series(1, $1) AS slot(n) + LEFT JOIN nodes ON n=slot_number + WHERE slot_number IS NULL + LIMIT 1', + # query label: + 'Node.available_slot_number', + # [col_id, val] for $1 vars: + [[nil, Rails.configuration.max_compute_nodes]], + ).rows.first.andand.first + end + def ensure_ping_secret self.info['ping_secret'] ||= rand(2**256).to_s(36) end @@ -173,6 +196,7 @@ class Node < ArvadosModel } if Rails.configuration.dns_server_conf_dir and Rails.configuration.dns_server_conf_template + tmpfile = nil begin begin template = IO.read(Rails.configuration.dns_server_conf_template) @@ -182,15 +206,20 @@ class Node < ArvadosModel end hostfile = File.join Rails.configuration.dns_server_conf_dir, "#{hostname}.conf" - tmpfile = Tempfile.open(["#{hostname}", ".conf.tmp"], + Tempfile.open(["#{hostname}-", ".conf.tmp"], Rails.configuration.dns_server_conf_dir) do |f| + tmpfile = f.path f.puts template % template_vars - f.path end File.rename tmpfile, hostfile rescue IOError, SystemCallError => e logger.error "Writing #{hostfile}: #{e.message}" ok = false + ensure + if tmpfile and File.file? tmpfile + # Cleanup remaining temporary file. + File.unlink tmpfile + end end end