A Golang implementation prototype for a backup task distribution

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The former section in the play with the Libtask author Russ Cox to Golang, a pile of heavyweight Daniel want to come out of something is different, the idea is very special, and the common difference is a bit big,

But the emphasis on practicality, the reduction of a bunch of grammar sugar, let people rethink, the development of language should be what? If used to do the service-side development, it is really a very good thing.

In one exercise, I designed a simple scenario for sending an Oracle database backup task. Practice Sync.waitgroup in Golang by sending backup tasks to individual clients.

sync. Waitgroup is a simple set of synchronization methods provided by Golang. It has three methods.
Add () adds a count that can be one or more.
Done () minus a count, if the count is not 0, wait () will block there until all 0
Wait () count is 0.

Get an example of an intuitive insight:

Package Mainimport ("FMT" "Sync") var waitgrp sync. Waitgroupfunc Main () {bakjobs: = []string{"Full Database", "Archive Log", "Control File"}for i: = 0; i < len (bakjobs); i++ {waitgrp.add (1)//add jobgo func (JobName string) {fmt. Println (JobName) waitgrp.done ()//sub Job} (Bakjobs[i])}waitgrp.wait ()}/*e:\github\golang\xcltools\src\test> Wgfull databasearchive Logcontrol file*/
You can see that the data in Bakjobs is printed in turn. But if the waitgrp.wait () is commented out. Nothing will output. That is, after the for, wait waits for all jobs to finish executing,

will be executed later. This example shows that, in addition to being familiar with the use of the next waitgroup, it also shows that waitgroup can guarantee the execution order of the job and will wait for all child jobs to complete before continuing.

In the case of Oracle database backup itself, in this use Go func () ... It doesn't make much sense, so I thought of a more complicated scenario, sending a batch of backup instructions to databases on multiple servers, and depending on the database

Order of importance to execute.

Here is the Code of Implementation (pure demo, ignoring other communication and logic processing aspects):

package Mainimport ("FMT" "Sync") var waitgrp sync. Waitgroupfunc Main () {Waitgrp.add (2) for _, IP: = range []string{"ip1", "Ip2"} {go Dojobs (IP) fmt. Println ("IP =", IP)}waitgrp.wait () fmt. Println ("Main () end.")} Func dojobs (IP string) {bakjobs: = []string{"Full Database", "Archive Log", "Control File"}for i: = 0; i < len (bakjobs) ; i++ {dobak (IP, I, bakjobs[i])}fmt. PRINTLN ("The Backup is complete!") Fmt. Println ("...") defer Waitgrp.done ()}func dobak (IP string, id int, jobName string) (bool, error) {FMT. Println ("Dobak ():", IP, "-", ID, "-", JobName) return true, Nil}/*ip = Ip1ip = Ip2dobak (): ip1-0-Full Datab   Asedobak (): Ip1-1-Archive Logdobak (): ip1-2-Control filethe Backup is complete!...... dobak (): ip2-0 -Full Databasedobak (): ip2-1-Archive Logdobak (): ip2-2-Control filethe backup was complete!...... main () end.*/

Code, the command to execute jobs is sent to each IP first. The execution time of each instruction is long and short, with Go func () ... can guarantee that at the same time to all the relevant IP issued a command to each IP to perform the relevant backup.

The master Goroutine waits for the total job to complete before executing the other. At the same time, due to the order of Waitgroup, can guarantee the important points of the IP, can be prioritized implementation.

The purpose of my design scenario is to add more Golang fresh elements to the example to enhance the feeling, so for the previous example, I envision a situation because database backups are time-consuming and wait for

It takes a long time for all the jobs on the IP to be executed, but what if the user asks to cancel the job?

So here's an example:

Package Mainimport ("Bufio" "FMT" "OS" "Sync" "Time") var waitgrp sync. Waitgroupfunc main () {ch: = make (chan bool) go Schedule (ch) r: = Bufio. Newreader (OS. Stdin) for {time. Sleep (time. Second) fmt. Print ("command:>") ln, _, _: = R.readline () cmd: = string (LN) if "q" = = cmd | | Quit "= = cmd {close (ch) break} else {fmt. Println ("= cmd =", cmd, "\ n")}}waitgrp.wait () fmt. Println ("Main () end.")} Func Schedule (CH chan bool) {for _, IP: = range []string{"ip1", "Ip2"} {waitgrp.add (1) Go dojobs (IP, ch) fmt. Println ("schedule () IP =", IP)}fmt. Println ("Schedule () end.") Return}func dojobs (IP string, ch chan bool) {defer waitgrp.done () for I: = 0; i <; i++ {select {case <-ch:fmt. Println ("Dojobs ()", IP, "=>job Cancel ...") returndefault:}fmt. Println ("Dojobs () ... ", IP," for: ", I) time. Sleep (time. Second)}}/*e:\github\golang\xcltools\src\test>wg5schedule () IP = ip1schedule () IP = ip2schedule () end.doJobs () ..... ip1 for:0dojobs () ... ip2 for:0command:> dojobs () ... ip1 for:1dojob (...)S () ..... ip2 for:1dojobs () ... ip1 for:2dojobs () ... ip2 for:2dojobs () ... ip1 for:3dojobs (...). IP2 (...) ...... For:3dojobs () ... ip1 for:4dojobs () ... ip2 for:4dojobs () ... ip1 for:5dojobs () ... ip2 for:5dojobs (......)  () ..... ip1 for:6dojobs () ... ip2 for:6dojobs () ... ip1 for:7dojobs () ... ip2 for:7dojobs (...). IP1 (...)----... For:8dojobs () ... ip2 for:8dojobs () ... ip1 for:9dojobs () ... ip2 for:9qmain () end. "." E:\github\golang\xcltools\src\test>wg5schedule () IP = ip1schedule () IP = ip2schedule () end.dojobs () ... ip1 for: 0doJobs () ..... ip2 for:0command:> dojobs () ... ip2 for:1dojobs () ... ip1 for:1dojobs (). Ip2 for:2 (...). Dojobs () ... ip1 for:2dojobs () ... ip2 for:3dojobs () ... ip1 for:3qdojobs () ip2 =>job cancel......dojob (...) S () ip1 =>job cancel......main () end.*/
I used a for{} to let the user enter a command to exit execution during the execution of the job. And, during the exit process, each IP will also be related to cancel processing. To ensure that no

There are some unnecessary problems with forced interruptions.

In this case, I have already introduced enough things, and I feel that I have achieved the purpose of the practice, and this is the first scene.

In this arrangement, for later investigation.


Mail:xcl_168@aliyun.com

blog:http://blog.csdn.net/xcl168


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.