Swift source resolution add device to ring

Source: Internet
Author: User
Execute swift-ring-builder Add command adds device to ring, similar to the Create command, the Add command is done by the Swift.cli.ringbuilder.Commands class Add () function
# swift/cli/ringbuilder.py def add (): #_parse_add_values () parse the parameter and return a list of device and check if the newly added device is in the devs list. #如果没有, the new device is added to the ring by the Add_dev () function of the Ringbuild class if Len (argv) < 5 or Len (argv)% 2! = 1:print Co Mmands.add.__doc__.strip () exit (Exit_error) for New_dev in _parse_add_values (argv[3:]): FO R Dev in Builder.devs:if Dev is none:continue if dev[' ip '] = = New_de v[' IP ' and \ dev[' port '] = = new_dev[' port '] and \ dev[' device '] = = New_de v[' device ': print ' device%d already uses%s:%d/%s. '% \ (dev[' id '], dev[' I
                    P '], dev[' Port '], dev[' device ') print "The On-disk ring Builder is unchanged.\n"
                  Exit (exit_error) dev_id = Builder.add_dev (new_dev) print (' Device%s with%s weight got ID%s '% (Format_Device (New_dev), new_dev[' weight '], dev_id)) Builder.save (Argv[1]) exit (exit_success) 
Finally, you finish adding the device using the Add_dev () function of the Swift.common.ring.builder.RingBuilder class instance created in the first step.
# swift/common/ring/builder.py def add_dev (self, dev): "" adds a device to the ring. The dict data for this device must contain at least the following key value (key) ====== =============================================================== ID Device A number (type is an integer), if "id" key is not specified in Dict, then the ID of the device is the next available ID number in the system weight the weight of the device.
        This weight value is used to indicate how many partition are assigned to this device.
        Region (an integer of type) where the area device resides. Zone number where the zone device is located (the type is an integer).
        A partition will be distributed as far as possible to different devices (Region,zone). IP Device IP Address port The TCP port of the device the device's name (for example, SDB1) meta metadata that is used to store user-defined data, such as device uptime, hardware description, and so on = = = = = = =============================================================== Note: Adding a device does not immediately cause rebalance,
                Because the user may want to do rebalance after adding multiple devices "" "If ' id ' not in dev:dev[' id '] = 0 if Self.devs: dev[' id ' = max (d[' id '] for D in Self.devs if d) + 1 if dev[' id '] < len (Self.devs) and Self.devs [dev[' ID ']] is not none:raise exceptions.
          Duplicatedeviceerror (      ' Duplicate Device ID:%d '% dev[' id ']) # ADD holes to Self.devs to ensure self.devs[dev[' ID '] "'ll be the D EV while dev[' id '] >= len (self.devs): Self.devs.append (None) dev[' weight '] = float (dev[' Wei Ght ']) dev[' parts '] = 0 self.devs[dev[' id ']] = Dev #根据设备的weight计算应该接纳的parttion个数 self._set _parts_wanted () self.devs_changed = True Self.version + = 1 return dev[' ID ']
This function first calculates that the Id,id value of the device to be added is not contiguous, allowing the presence of voids in the middle of the equipment table. Then add the device to the ring's device table, and finally set the relevant flag,devs_changed to indicate that the device table has changed and needs to be rebalance. When this function returns, the Add () function of the Swift.cli.ringbuilder.Commands class calls the Save () function of the Ringbuild class again to write the updated ring information to the builder file. The steps to add the device to the ring are complete.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.