Skip to content

Commit

Permalink
add compaction IO speed limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
ideawu committed Jun 15, 2013
1 parent 5c7115b commit a746350
Show file tree
Hide file tree
Showing 12 changed files with 1,758 additions and 11 deletions.
30 changes: 27 additions & 3 deletions api/php/SSDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,38 @@ function __construct($host, $port, $timeout_ms=200){
if(!$this->sock){
throw new Exception(socket_strerror(socket_last_error()));
}

socket_set_nonblock($this->sock);
while(1){
if($timeout_ms < 0){
throw new Exception("Connection timeout!");
}

$ret = @socket_connect($this->sock, $host, $port);
if($ret){
break;
}
$err = socket_last_error($this->sock);
if($err == SOCKET_EISCONN){
break;
}

usleep(100 * 1000);
$timeout_ms -= 100;
}
socket_set_block($this->sock);

$timeout_sec = intval($timeout_ms/1000);
$timeout_ms = $timeout_ms - $timeout_sec * 1000;
@socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$timeout_sec, 'usec'=>$timeout_ms));
@socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>$timeout_sec, 'usec'=>$timeout_ms));
$timeout_usec = ($timeout_ms - $timeout_sec * 1000) * 1000;
//@socket_set_timeout($this->sock, $timeout_sec, $timeout_ms);
@socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$timeout_sec, 'usec'=>$timeout_usec));
@socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>$timeout_sec, 'usec'=>$timeout_usec));
/*
$ret = @socket_connect($this->sock, $host, $port);
if(!$ret){
throw new Exception(socket_strerror(socket_last_error()));
}
*/
if(is_int(TCP_NODELAY)){
socket_set_option($this->sock, SOL_TCP, TCP_NODELAY, 1);
}
Expand Down
16 changes: 16 additions & 0 deletions deps/leveldb-1.9.0/db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,22 @@ Status DBImpl::FinishCompactionOutputFile(CompactionState* compact,
(unsigned long long) output_number,
(unsigned long long) current_entries,
(unsigned long long) current_bytes);

// Writing current_bytes to disk is considered no expense(cost no time),
// so we calculate how many IOs will match the compaction speed,
// then sleep 1s/IOs
// Added by [email protected]
int mbs = current_bytes/1024/1024;
if(options_.compaction_speed > 0 && mbs > 1){
int count = options_.compaction_speed/mbs;
if(count < 1){
count = 1;
}
int pause = 1000 * 1000 / count;
Log(options_.info_log, "compaction_speed: %d MB, pause: %d us",
options_.compaction_speed, pause);
env_->SleepForMicroseconds(pause);
}
}
}
return s;
Expand Down
Loading

0 comments on commit a746350

Please sign in to comment.