github.com/annchain/OG@v0.0.9/benchmark/auto/auto_client_manager.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package auto 15 16 import ( 17 "github.com/annchain/OG/account" 18 "github.com/annchain/OG/arefactor/common/goroutine" 19 "github.com/annchain/OG/node" 20 "github.com/annchain/OG/og" 21 "github.com/annchain/OG/og/types" 22 "github.com/sirupsen/logrus" 23 "github.com/spf13/viper" 24 "sync" 25 ) 26 27 type AutoClientManager struct { 28 Clients []*AutoClient 29 SampleAccounts []*account.Account 30 UpToDateEventListener chan bool 31 NodeStatusDataProvider og.NodeStatusDataProvider 32 quit chan bool 33 wg sync.WaitGroup 34 RegisterReceiver func(c chan types.Txi) 35 delegate *node.Delegate 36 } 37 38 func (m *AutoClientManager) Init(accountIndices []int, delegate *node.Delegate, coinBaseAccount *account.Account) { 39 m.Clients = []*AutoClient{} 40 m.UpToDateEventListener = make(chan bool) 41 m.quit = make(chan bool) 42 m.delegate = delegate 43 // to make sure we have only one sequencer per node 44 sequencers := 1 45 mode := viper.GetString("mode") 46 var tpsTest bool 47 if mode == "tps_test" { 48 tpsTest = true 49 accountIndices = []int{0} 50 } 51 for _, accountIndex := range accountIndices { 52 client := &AutoClient{ 53 Delegate: delegate, 54 SampleAccounts: m.SampleAccounts, 55 MyIndex: accountIndex, 56 MyAccount: m.SampleAccounts[accountIndex], 57 NonceSelfDiscipline: viper.GetBool("auto_client.nonce_self_discipline"), 58 IntervalMode: viper.GetString("auto_client.tx.interval_mode"), 59 SequencerIntervalUs: viper.GetInt("auto_client.sequencer.interval_us"), 60 TxIntervalUs: viper.GetInt("auto_client.tx.interval_us"), 61 AutoTxEnabled: viper.GetBool("auto_client.tx.enabled"), 62 AutoSequencerEnabled: viper.GetBool("auto_client.sequencer.enabled") && sequencers > 0 && accountIndex == 0, 63 AutoArchiveEnabled: viper.GetBool("auto_client.archive.enabled"), 64 ArchiveInterValUs: viper.GetInt("auto_client.archive.interval_us"), 65 TpsTest: tpsTest, 66 TestInsertPool: viper.GetBool("auto_client.tx.test_insert_pool"), 67 TestDagPush: viper.GetBool("auto_client.tx.test_dag_push"), 68 TestSyncBuffer: viper.GetBool("auto_client.tx.test_sync_buffer"), 69 TestSeal: viper.GetBool("auto_client.tx.test_seal"), 70 } 71 Init() 72 m.Clients = append(m.Clients, client) 73 if AutoSequencerEnabled { 74 sequencers-- 75 } 76 77 } 78 if !tpsTest && sequencers != 0 && viper.GetBool("auto_client.sequencer.enabled") { 79 // add pure sequencer, never produce tx 80 client := &AutoClient{ 81 Delegate: delegate, 82 SampleAccounts: m.SampleAccounts, 83 MyAccount: m.SampleAccounts[0], 84 NonceSelfDiscipline: viper.GetBool("auto_client.nonce_self_discipline"), 85 IntervalMode: viper.GetString("auto_client.tx.interval_mode"), 86 SequencerIntervalUs: viper.GetInt("auto_client.sequencer.interval_us"), 87 TxIntervalUs: viper.GetInt("auto_client.tx.interval_us"), 88 AutoTxEnabled: false, // always false. If a sequencer is also a tx maker, it will be already added above 89 AutoSequencerEnabled: true, 90 AutoArchiveEnabled: viper.GetBool("auto_client.archive.enabled"), 91 ArchiveInterValUs: viper.GetInt("auto_client.archive.interval_us"), 92 TestInsertPool: viper.GetBool("auto_client.tx.test_insert_pool"), 93 TestDagPush: viper.GetBool("auto_client.tx.test_dag_push"), 94 TestSyncBuffer: viper.GetBool("auto_client.tx.test_sync_buffer"), 95 TestSeal: viper.GetBool("auto_client.tx.test_seal"), 96 } 97 Init() 98 m.Clients = append(m.Clients, client) 99 } 100 101 if !tpsTest && viper.GetBool("annsensus.campaign") { 102 // add pure sequencer 103 client := &AutoClient{ 104 Delegate: delegate, 105 SampleAccounts: m.SampleAccounts, 106 MyAccount: coinBaseAccount, 107 NonceSelfDiscipline: viper.GetBool("auto_client.nonce_self_discipline"), 108 IntervalMode: viper.GetString("auto_client.tx.interval_mode"), 109 SequencerIntervalUs: viper.GetInt("auto_client.sequencer.interval_us"), 110 TxIntervalUs: viper.GetInt("auto_client.tx.interval_us"), 111 AutoTxEnabled: false, // always false. If a sequencer is also a tx maker, it will be already added above 112 AutoSequencerEnabled: false, 113 AutoArchiveEnabled: viper.GetBool("auto_client.archive.enabled"), 114 ArchiveInterValUs: viper.GetInt("auto_client.archive.interval_us"), 115 TestInsertPool: viper.GetBool("auto_client.tx.test_insert_pool"), 116 TestDagPush: viper.GetBool("auto_client.tx.test_dag_push"), 117 TestSyncBuffer: viper.GetBool("auto_client.tx.test_sync_buffer"), 118 CampainEnable: true, 119 } 120 Init() 121 m.RegisterReceiver(NewRawTx) 122 m.Clients = append(m.Clients, client) 123 } 124 } 125 126 func (m *AutoClientManager) SetTxIntervalUs(interval int) { 127 for i := 0; i < len(m.Clients); i++ { 128 if m.Clients[i] != nil { 129 SetTxIntervalUs(interval) 130 } 131 } 132 } 133 134 func (m *AutoClientManager) Start() { 135 for _, client := range m.Clients { 136 m.wg.Add(1) 137 Start() 138 } 139 m.wg.Add(1) 140 goroutine.New(m.eventLoop) 141 } 142 143 func (m *AutoClientManager) Stop() { 144 close(m.quit) 145 m.wg.Wait() 146 } 147 148 func (m *AutoClientManager) Name() string { 149 return "AutoClientManager" 150 } 151 152 func (c *AutoClientManager) eventLoop() { 153 defer c.wg.Done() 154 for { 155 select { 156 case v := <-c.UpToDateEventListener: 157 for _, client := range c.Clients { 158 if !v { 159 logrus.Info("pausing client") 160 Pause() 161 } else { 162 logrus.Info("resuming client") 163 Resume() 164 } 165 } 166 //case <-time.After(time.Second * 20): 167 //continue 168 case <-c.quit: 169 logrus.Info("got quit signal") 170 // wait for all clients to stop 171 for _, client := range c.Clients { 172 Stop() 173 c.wg.Done() 174 } 175 return 176 } 177 178 } 179 180 }