Shantanu's Blog

Corporate Consultant

May 12, 2013

 

Meta Data Locking

How do I know which connection is holding on to the lock?

Identifying what user and thread are holding on to a meta data lock that is preventing other queries from running is not easily traceable. In some cases, a "stale" transaction holds a row lock.

mysql> select trx_mysql_thread_id, trx_started from information_schema.innodb_trx where trx_rows_locked > 0 and trx_query IS NULL order by trx_started;
+---------------------+---------------------+
| trx_mysql_thread_id | trx_started |
+---------------------+---------------------+
| 1 | 2012-06-15 13:48:20 |
| 5 | 2012-06-15 13:58:26 |
+---------------------+---------------------+

mysql> select now() - trx_started as active_period, trx_mysql_thread_id, trx_started from information_schema.innodb_trx where trx_rows_locked > 0 and trx_query IS NULL and (now() - trx_started > 600) order by trx_started;
+---------------+---------------------+---------------------+
| active_period | trx_mysql_thread_id | trx_started |
+---------------+---------------------+---------------------+
| 5689.000000 | 1 | 2012-06-15 13:48:20 |
| 4683.000000 | 5 | 2012-06-15 13:58:26 |
+---------------+---------------------+---------------------+

Labels:


April 24, 2013

 

SQLite with Python examples

import sqlite3
conn = sqlite3.connect('/root/accounts.db', timeout=10)
c = conn.cursor()

c.execute('''CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Do this instead
t = ('2013-01-05','BUY','RHAT',100,35.14)

c.execute('INSERT INTO stocks VALUES (?,?,?,?,?)', t)

# Larger example that inserts many records at a time
purchases = [('2014-03-28', 'BUY', 'IBM', 1000, 45.00),
             ('2015-04-05', 'BUY', 'MSFT', 1000, 72.00),
             ('2016-04-06', 'SELL', 'IBM', 500, 53.00),
            ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)

# print all rows
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
        print row

conn.commit()
conn.close()

_____

conn = sqlite3.connect('/Users/test/Desktop/my-accounts.db')
currentAccount = None

for row in conn.execute('SELECT email FROM accounts WHERE active=0'):
    currentAccount = row[0]
    print "Checking out: ",currentAccount
    break

if currentAccount is None:
    print "No available accounts"
else:
    conn.execute('UPDATE accounts SET active=1 WHERE email=?', [currentAccount,])
conn.close()

conn.commit()

_____

# A minimal SQLite shell for experiments

import sqlite3

con = sqlite3.connect(":memory:")
con.isolation_level = None
cur = con.cursor()

buffer = ""

print "Enter your SQL commands to execute in sqlite3."
print "Enter a blank line to exit."

while True:
    line = raw_input()
    if line == "":
        break
    buffer += line
    if sqlite3.complete_statement(buffer):
        try:
            buffer = buffer.strip()
            cur.execute(buffer)

            if buffer.lstrip().upper().startswith("SELECT"):
                print cur.fetchall()
        except sqlite3.Error as e:
            print "An error occurred:", e.args[0]
        buffer = ""

con.close()

_____


sqlite3 is the obvious alternative for a persistent store in python.
But its powerful SQL interface can be too complex when you just want a dict.

import sqlite3dbm
db = sqlite3dbm.sshelve.open('mydb.sqlite3')
db['foo'] = {'count': 100, 'ctr': .3}
db['bar'] = {'count': 314, 'ctr': .168}
db.items()
[('foo', {'count': 100, 'ctr': 0.29999999999999999}), ('bar', {'count': 314, 'ctr': 0.16800000000000001})]

db['foo']['count']
100

// if dbm is not installed...
easy_install sqlite3dbm

Labels: ,


 

Manage DynamoDB using python

// connect
import boto.dynamodb
conn = boto.connect_dynamodb (aws_access_key_id='', aws_secret_access_key='')

// list tables
conn.list_tables()
[u'MTA', u'reply']

// select table
table = conn.get_table('reply')

// select a record
item = table.get_item(hash_key='Amazon DynamoDB#DynamoDB Thread 2', range_key='2012-01-31 23:28:40')
print item

// update record
item['PostedBy'] = 'User X'
item.put()

// delete record
item.delete()

// add record
item_data = {
        'Message': 'DynamoDB Thread 3 Reply 4 text',
        'SentBy': 'User A',
        'PostedBy': 'User oksoft',
        'ReceivedTime': '12/9/2011 11:36:03 PM',
    }

item = table.new_item(
        # Our hash key is 'forum'
        hash_key='Amazon DynamoDB#DynamoDB Thread 3',
        # Our range key is 'subject'
        range_key='2013-04-24 23:28:40',
        # This has the
        attrs=item_data
    )

item.put()

Labels: ,


April 21, 2013

 

S3 versioning

It is possible to recover a deleted file if S3 versioning is enabled.
We simply need to find the version ID of the deleted file and restore it back on S3.

import boto
c = boto.connect_s3('access-key','secret-key')
bucket = c.get_bucket('todelapr15')

rs = bucket.list_versions()
for key in rs:
  print key, key.version_id

The above code will list all the files and their version ID as shown below.

74eHPzvO1HpNvyujXn1.hs.UG5yObfh_
p7uMSJjrNLwJbclPPbYo56TPyb1L4asu
gjhBuYB4RgpwGJt6DCbIFHVBX7Od_hP.
IoBQ7FB1h4kDJrvzLlDmhPtBRts.14RF
WN4oSvCvoOmfmnAPT5pZS.YFCohyrvjh
Im3ci98wSsng8vaAZNDBsw8hoaTYcR2l

The deleted file needs to be copied first to a new location before it can be downloaded.


bucket.copy_key('new_ptty.exe', 'todelapr15/testing', 'ptty.exe', metadata=None, src_version_id='WN4oSvCvoOmfmnAPT5pZS.YFCohyrvjh')

Labels: ,


 

python and YAML


YAML is a human friendly data serialization standard for all programming languages including python.

>>> import yaml
>>> document = """
  a: 1
  b:
    c: 3
    d: 4
"""

>>>print yaml.dump(yaml.load(document))
a: 1
b: {c: 3, d: 4}

>>> print yaml.dump(yaml.load(document), default_flow_style=False)
a: 1
b:
  c: 3
  d: 4

https://pypi.python.org/pypi/PyYAML

Labels:


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013  

This page is powered by Blogger. Isn't yours?