-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from halprin/try-conurrency
Limit Concurrency
- Loading branch information
Showing
5 changed files
with
177 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package dynamo | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"log" | ||
"math" | ||
"runtime" | ||
) | ||
|
||
func determineConcurrency(tableName string) (int, error) { | ||
tableInfo, err := describeTable(tableName) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if isOnDemand(tableInfo) { | ||
concurrency := getOnDemandConcurrency() | ||
log.Printf("Given on demand concurrency, set concurrency to %d\n", concurrency) | ||
return concurrency, nil | ||
} | ||
|
||
//the table has provisioned capacity | ||
|
||
numberOfItems := getNumberOfItems(tableInfo) | ||
tableSize := getTableSizeInBytes(tableInfo) | ||
|
||
roundedUpAverageItemSize := calculateAverageItemSize(tableSize, numberOfItems) | ||
totalBatchSize := float64(maxItemsPerBatchRequest) * roundedUpAverageItemSize | ||
|
||
writeCapacityUnits := getWriteCapacityUnits(tableInfo) | ||
rawConcurrency := float64(writeCapacityUnits) / totalBatchSize | ||
|
||
concurrency := 1 | ||
if rawConcurrency > 1 { | ||
//possible truncation to size of int | ||
concurrency = int(rawConcurrency) | ||
} | ||
|
||
log.Printf("Given provisioned write capacity of %d, number of items %d, and table size %f KB, set concurrency to %d\n", writeCapacityUnits, numberOfItems, float64(tableSize) / float64(1024), concurrency) | ||
|
||
return concurrency, nil | ||
} | ||
|
||
func isOnDemand(describeTable *dynamodb.DescribeTableOutput) bool { | ||
billingModeSummary := describeTable.Table.BillingModeSummary | ||
if billingModeSummary != nil { | ||
return *describeTable.Table.BillingModeSummary.BillingMode == dynamodb.BillingModePayPerRequest | ||
} | ||
|
||
return getWriteCapacityUnits(describeTable) == 0 | ||
} | ||
|
||
func getWriteCapacityUnits(describeTable *dynamodb.DescribeTableOutput) int64 { | ||
return *describeTable.Table.ProvisionedThroughput.WriteCapacityUnits | ||
} | ||
|
||
func getNumberOfItems(describeTable *dynamodb.DescribeTableOutput) int64 { | ||
return *describeTable.Table.ItemCount | ||
} | ||
|
||
func getTableSizeInBytes(describeTable *dynamodb.DescribeTableOutput) int64 { | ||
return *describeTable.Table.TableSizeBytes | ||
} | ||
|
||
func getOnDemandConcurrency() int { | ||
//on demand's concurrency is the number of logical CPUs | ||
return runtime.NumCPU() | ||
} | ||
|
||
func calculateAverageItemSize(tableSize int64, numberOfItems int64) float64 { | ||
if tableSize == 0 || numberOfItems == 0 { | ||
//possible for the size or number of items to be 0 since they aren't always updated | ||
return 1.0 | ||
} | ||
|
||
//truncates some of the digits if tableSize is too big | ||
//1024 to get Kilobytes | ||
averageItemSize := float64(tableSize) / float64(1024) / float64(numberOfItems) | ||
return math.Ceil(averageItemSize) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package parallel | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Pool struct { | ||
ingestionPoolChannel chan func() | ||
executionPoolChannel chan func() | ||
waitGroup sync.WaitGroup | ||
} | ||
|
||
//poolSize needs to be bigger than taskQueueSize | ||
func NewPool(poolSize int, taskQueueSize int) *Pool { | ||
newPool := &Pool{ | ||
ingestionPoolChannel: make(chan func(), taskQueueSize), | ||
executionPoolChannel: make(chan func(), poolSize), | ||
} | ||
|
||
go newPool.submitIngestionGoroutine() | ||
for workerIndex := 0; workerIndex < poolSize; workerIndex++ { | ||
go newPool.submitExecutionGoroutine() | ||
} | ||
|
||
return newPool | ||
} | ||
|
||
func (pool *Pool) Submit(task func()) { | ||
pool.ingestionPoolChannel <- task | ||
} | ||
|
||
func (pool *Pool) Release() { | ||
close(pool.ingestionPoolChannel) | ||
close(pool.executionPoolChannel) | ||
} | ||
|
||
func (pool *Pool) submitIngestionGoroutine() { | ||
for submittedTask := range pool.ingestionPoolChannel { | ||
pool.executionPoolChannel <- submittedTask | ||
} | ||
} | ||
|
||
func (pool *Pool) submitExecutionGoroutine() { | ||
for submittedTask := range pool.executionPoolChannel { | ||
submittedTask() | ||
} | ||
} |