| Conditions | 19 |
| Paths | 184 |
| Total Lines | 108 |
| Code Lines | 65 |
| Lines | 10 |
| Ratio | 9.26 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 82 | protected function getServerStates( array $serverIndexes, $domain ) { |
||
| 83 | $writerIndex = $this->parent->getWriterIndex(); |
||
| 84 | if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == $writerIndex ) { |
||
| 85 | # Single server only, just return zero without caching |
||
| 86 | return [ |
||
| 87 | 'lagTimes' => [ $writerIndex => 0 ], |
||
| 88 | 'weightScales' => [ $writerIndex => 1.0 ] |
||
| 89 | ]; |
||
| 90 | } |
||
| 91 | |||
| 92 | $key = $this->getCacheKey( $serverIndexes ); |
||
| 93 | # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec) |
||
| 94 | $ttl = mt_rand( 4e6, 5e6 ) / 1e6; |
||
| 95 | # Keep keys around longer as fallbacks |
||
| 96 | $staleTTL = 60; |
||
| 97 | |||
| 98 | # (a) Check the local APC cache |
||
| 99 | $value = $this->srvCache->get( $key ); |
||
| 100 | if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) { |
||
| 101 | $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from local cache" ); |
||
| 102 | return $value; // cache hit |
||
| 103 | } |
||
| 104 | $staleValue = $value ?: false; |
||
| 105 | |||
| 106 | # (b) Check the shared cache and backfill APC |
||
| 107 | $value = $this->mainCache->get( $key ); |
||
| 108 | if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) { |
||
| 109 | $this->srvCache->set( $key, $value, $staleTTL ); |
||
| 110 | $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from main cache" ); |
||
| 111 | |||
| 112 | return $value; // cache hit |
||
| 113 | } |
||
| 114 | $staleValue = $value ?: $staleValue; |
||
| 115 | |||
| 116 | # (c) Cache key missing or expired; regenerate and backfill |
||
| 117 | if ( $this->mainCache->lock( $key, 0, 10 ) ) { |
||
| 118 | # Let this process alone update the cache value |
||
| 119 | $cache = $this->mainCache; |
||
| 120 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
| 121 | $unlocker = new ScopedCallback( function () use ( $cache, $key ) { |
||
| 122 | $cache->unlock( $key ); |
||
| 123 | } ); |
||
| 124 | } elseif ( $staleValue ) { |
||
| 125 | # Could not acquire lock but an old cache exists, so use it |
||
| 126 | return $staleValue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $lagTimes = []; |
||
| 130 | $weightScales = []; |
||
| 131 | $movAveRatio = $this->movingAveRatio; |
||
| 132 | foreach ( $serverIndexes as $i ) { |
||
| 133 | if ( $i == $this->parent->getWriterIndex() ) { |
||
| 134 | $lagTimes[$i] = 0; // master always has no lag |
||
| 135 | $weightScales[$i] = 1.0; // nominal weight |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | |||
| 139 | $conn = $this->parent->getAnyOpenConnection( $i ); |
||
| 140 | if ( $conn ) { |
||
| 141 | $close = false; // already open |
||
| 142 | } else { |
||
| 143 | $conn = $this->parent->openConnection( $i, $domain ); |
||
| 144 | $close = true; // new connection |
||
| 145 | } |
||
| 146 | |||
| 147 | $lastWeight = isset( $staleValue['weightScales'][$i] ) |
||
| 148 | ? $staleValue['weightScales'][$i] |
||
| 149 | : 1.0; |
||
| 150 | $coefficient = $this->getWeightScale( $i, $conn ?: null ); |
||
| 151 | $newWeight = $movAveRatio * $coefficient + ( 1 - $movAveRatio ) * $lastWeight; |
||
| 152 | |||
| 153 | // Scale from 10% to 100% of nominal weight |
||
| 154 | $weightScales[$i] = max( $newWeight, .10 ); |
||
| 155 | |||
| 156 | View Code Duplication | if ( !$conn ) { |
|
| 157 | $lagTimes[$i] = false; |
||
| 158 | $host = $this->parent->getServerName( $i ); |
||
| 159 | $this->replLogger->error( __METHOD__ . ": host $host is unreachable" ); |
||
| 160 | continue; |
||
| 161 | } |
||
| 162 | |||
| 163 | $lagTimes[$i] = $conn->getLag(); |
||
| 164 | View Code Duplication | if ( $lagTimes[$i] === false ) { |
|
| 165 | $host = $this->parent->getServerName( $i ); |
||
| 166 | $this->replLogger->error( __METHOD__ . ": host $host is not replicating?" ); |
||
| 167 | } |
||
| 168 | |||
| 169 | if ( $close ) { |
||
| 170 | # Close the connection to avoid sleeper connections piling up. |
||
| 171 | # Note that the caller will pick one of these DBs and reconnect, |
||
| 172 | # which is slightly inefficient, but this only matters for the lag |
||
| 173 | # time cache miss cache, which is far less common that cache hits. |
||
| 174 | $this->parent->closeConnection( $conn ); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | # Add a timestamp key so we know when it was cached |
||
| 179 | $value = [ |
||
| 180 | 'lagTimes' => $lagTimes, |
||
| 181 | 'weightScales' => $weightScales, |
||
| 182 | 'timestamp' => microtime( true ) |
||
| 183 | ]; |
||
| 184 | $this->mainCache->set( $key, $value, $staleTTL ); |
||
| 185 | $this->srvCache->set( $key, $value, $staleTTL ); |
||
| 186 | $this->replLogger->info( __METHOD__ . ": re-calculated lag times ($key)" ); |
||
| 187 | |||
| 188 | return $value; |
||
| 189 | } |
||
| 190 | |||
| 211 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.